0% found this document useful (0 votes)
19 views

JAVA

The RequestDispatcher interface is used to forward requests between servlets and JSP pages. It has two methods - forward() and include() - that allow transferring control to another resource. JDBC is used to connect to MySQL databases in Java by registering the driver, creating a Connection object, creating a Statement object, executing queries, and extracting results. Cookies are used for session tracking in servlets and allow recognizing users across requests by storing identifier values in the browser.

Uploaded by

Atharva Inamdar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

JAVA

The RequestDispatcher interface is used to forward requests between servlets and JSP pages. It has two methods - forward() and include() - that allow transferring control to another resource. JDBC is used to connect to MySQL databases in Java by registering the driver, creating a Connection object, creating a Statement object, executing queries, and extracting results. Cookies are used for session tracking in servlets and allow recognizing users across requests by storing identifier values in the browser.

Uploaded by

Atharva Inamdar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

What is the purpose of RequestDispatcher Interface?

The RequestDispatcher is an Interface that comes under


package javax. servlet. Using this interface we get an object
in servlet after receiving the request. Using the
RequestDispatcher object we send a request to other
resources which include (servlet, HTML file, or JSP file).
What are the two methods in RequestDispatcher interface?

The forward() method is used to transfer the client request to


another resource (HTML file, servlet, jsp etc). When this
method is called, the control is transferred to the next
resource called. On the other hand, the include() method is
used to include the content of the calling file into the called
file
Q.) How do we connect to MYSQL data through JDBC?
To connect to MySQL data through JDBC in Java, you need to:
1. Register the JDBC driver.
2. Create a Connection object.
3. Create a Statement object.
4. Execute a query.
5. Extract data from the result set.
URL for Connection:- The connection URL for the mysql
database is jdbc:mysql://localhost:3306/mydb ('mydb' is the
name of database). Specify to the DriverManager which JDBC
drivers to try to make Connections use below line. Class.
forName("com.
What is Session Tracking in Servlet? Explain any one
technique to track the session?
Session tracking is a mechanism that servlets use to maintain state
about a series of requests from the same user. It's also known as
session management in servlet.
Session tracking is used to:
 Store information on specific users
 Recognize these user's requests when they connect to the web
server
 Maintain state about a series of requests from the same user
 Share sessions among the servlets accessed by a client
Sessions in Java are managed through different ways, such as:
 HTTP Session API
 Cookies
 URL rewriting
One technique to track the session is:
 Hidden form field
Insert information in the webpages and send this information to the
server. These fields are not viewable to the user directly.
 Cookie
The server sends a key value pair of information to the browser. The
browser then sends back this identifier to the server with every
request.

Explain JSP taglib directive in brief?


JSP - The taglib DirectiveThe taglib directive declares that your JSP
page uses a set of custom tags, identifies the location of the library,
and provides means for identifying the custom tags in your JSP page.
The JSP taglib directive defines a tag library that can be used in JSP. It
declares that a JSP page uses a set of custom tags, identifies the
location of the library, and provides means for identifying the custom
tags in the JSP page.
The syntax for the taglib directive is:
 <%@ taglib uri = "uri" prefix = "prefixOfTag" >

What is EL in JSP? Explain with example?


JSP Expression Language (EL) makes it easy to access the application
for the data stored in the javabeans components. It also allows
create the expressions which are both arithmetic and logical. Within
EL tags we can use integers. Floating point numbers, strings and
Boolean values.
EL uses simple syntax to access beans, such as:
 ${name} for a simple variable
 ${name. foo. bar} for a nested property
EL tags can use:
 Integers
 Floating point numbers
 Strings
 Boolean values
What is an example of El in JSP?
EL expressions can use parentheses to group subexpressions. For
example, ${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} equals 7. The valid
values of this attribute are true and false.

Differentiate between statement & prepared


statement interface.
Statement PreparedStatement

It is used when SQL query is to be It is used when SQL query is to be


executed only once. executed multiple times.

You can not pass parameters at


runtime. You can pass parameters at runtime.

Used for CREATE, ALTER, Used for the queries which are to be
DROP statements. executed multiple times.

Performance is very low. Performance is better than Statemen t.

It is base interface. It extends statement interface.

Used to execute normal SQL


queries. Used to execute dynamic SQL queries.

We can not use statement for We can use Preparedstatement for


reading binary data. reading binary data.

It is used for DDL statements. It is used for any SQL Query.

We can not use statement for We can use Preparedstatement for


writing binary data. writing binary data.

No binary protocol is used for


communication. Binary protocol is used for communi

Explain and write the syntax of getcookies ( ) method in


servlet?
Cookies in Servlet
A cookie is a small piece of information that is persisted between the multiple client
requests.

A cookie has a name, a single value, and optional attributes such as a comment, path
and domain qualifiers, a maximum age, and a version number.

How Cookie works


By default, each request is considered as a new request. In cookies technique, we add
cookie with response from the servlet. So cookie is stored in the cache of the
browser. After that if request is sent by the user, cookie is added with request by
default. Thus, we recognize the user as the old user.

To make a cookie, create an object of Cookie class and pass a name


and its value. To add cookie in response, use addCookie(Cookie)
method of HttpServletResponse interface. To fetch the cookie,
getCookies() method of Request Interface is used

1. Write a JSP application for Student Registration for


Hackathon Event?
(Use JDBC and Mysql)
1. Open MySQL Workbench.
2. Create a new database named "hackathon".
3. Create a table named "students" with the following columns:
o id: The student's ID number
o name: The student's name
o email: The student's email address
o phone: The student's phone number
o school: The student's school
o major: The student's major
o year: The student's year of study

2. Write a Servlet application to Track the user(Use Cookies)


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TrackUser extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {

// Get the user's name from the request.


String name = request.getParameter("name");

// Create a new cookie with the user's name.


Cookie cookie = new Cookie("name", name);
cookie.setMaxAge(3600); // Set the cookie to expire after 1 hour.

// Add the cookie to the response.


response.addCookie(cookie);

// Redirect the user to a new page.


response.sendRedirect("/index.jsp");
}

public void doGet(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {

// Get the user's cookie.


Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("name")) {
// Get the user's name from the cookie.
String name = cookie.getValue();

// Print the user's name to the response.


PrintWriter out = response.getWriter();
out.println("Hello, " + name + "!");
out.close();
return;
}
}
}

You might also like