CSC

Explain the various scripting elements used in JSP.

JSP is a technology used to create dynamic web content, and it incorporates Java code within HTML or XML documents. There are three main scripting elements in JSP:


1. Scriptlets: Scriptlets are blocks of Java code embedded within a JSP page. They are enclosed between <% and %> tags. The code within scriptlets is executed when the JSP page is requested, and it can be used to perform various tasks, such as setting variables, performing calculations, or interacting with databases.

Example:

```
<% int x = 10; %>
```
2. Expressions: Expressions are used to output the result of a Java expression directly to the HTML or XML document. They are enclosed between <%= and %> tags. Expressions are evaluated and converted to a string before being inserted into the output..
Example:

```
The value of x is: <%= x %>
```
3. Declarations: Declarations are used to define variables, methods, or classes that can be used throughout the JSP page. They are enclosed between <%! and %> tags. Declarations are useful for defining reusable code or creating custom functions.
Example:

```
<%! int multiply(int a, int b) {
return a * b;
} %>
```

What is a Java Bean? How is it different from a normal java object? Which tag is use to use a Java Bean in JSP. Give the line of code to use a bean in JSP.

• A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications. • Following are the unique characteristics that distinguish a JavaBean from other Java classes: • 1 )It is public • 2) It provides a public default, no-argument constructor. • 3) It should be serializable and implement the Serializable interface. (NOT COMPULSORY) • 4) It may have a number of properties which can be read or written. (these properties are actually data members of that class) • 5) It may have a number of "getter" and "setter" methods for the properties. • If a property has only a getter method it is read only • If a property has only a setter method it is write only • If it has both getter and setter methods it is read write • It can have other normal methods like an ordinary class • These methods can be called in the JSP that uses the bean

JavaBeans Properties • A JavaBean property is actually a data member/attribute of the bean class • This data member/property has a name • This property can be accessed by the user of the bean object, with its name • The attribute can be of any Java data type, including classes that you define. • A JavaBean property may be read, write, read only, or write only.

To use a Java Bean in a JSP, you can use the `<jsp:useBean>` tag. This tag is used to instantiate a bean and associate it with a variable name in the JSP. Here's an example of how to use the `<jsp:useBean>` tag to instantiate a bean in JSP: ```jsp <jsp:useBean id="myBean" class="com.example.MyBean" scope="request" /> ``` In this example: - `id` specifies the variable name that will be used to refer to the bean instance (`myBean` in this case). - `class` specifies the fully qualified name of the bean class (`com.example.MyBean` in this case). - `scope` determines the scope of the bean instance (e.g., `request`, `session`, `application`, etc.). After using the `<jsp:useBean>` tag, you can access the properties and invoke methods of the bean using the variable name assigned to it. Note that to use a Java Bean in a JSP, you need to have the bean class available in the classpath of your web application.

What are java script objects?

What are different ways of creating a javascript objects Explain.

In JavaScript, almost "everything" is an object. 
•Booleans can be objects (if defined with the new keyword) 
•Numbers can be objects (if defined with the new keyword) 
•Strings can be objects (if defined with the new keyword) •Dates are always objects 
•Regular expressions are always objects 
•Arrays are always objects 
•Functions are always objects 
All JavaScript values, except primitives, are objects.
JavaScript Objects 
• is template based not class based. 
• In JavaScript we don't create class to get the object. But, we directly create objects. 
• Objects are variables too. But objects can contain many values. 
• The values are written as name : value pairs (name and value separated by a colon). 
• Example • var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}

Creating Objects in JavaScript 

JavaScript Object by object literal
• object={property1:value1,property2:value2.....property
N:valueN}
• Example:
1.<script>
2.emp={id:102,name:"Shyam Kumar",salary:40000}
3.document.write(emp.id+" "+emp.name+" "+emp.salar
y);
4.</script

By creating instance of Object
• var objectname=new Object();
• Example:
1.<script>
2.var emp=new Object();
3.emp.id=101;
4.emp.name="Ravi Malik";
5.emp.salary=50000;
6.document.write(emp.id+" "+emp.name+" "+emp.salary);
7.</script



Write a javascript function to check validity of a phone number

function isValidPhoneNumber(phoneNumber) {
• // Regular expression to match phone number pattern
• const pattern ="^[+][0-9]{12}"
• // Check if the phone number matches the pattern
• if (phoneNumber.match(pattern)) {
• return true;
• } else {
• return false;
• }
• }

1 Comments

Contact Form