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

Lab 02 Slides

Uploaded by

nada abdelrahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 02 Slides

Uploaded by

nada abdelrahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Servlets

JSP Life Cycle : JSP are translated into Servlets


Servlets - What are they?
Servlets are objects of Java classes which resides in a web server in order
to respond to HTTP requests. Servlets may return data of any type but they
often return HTML.

All servlets classes must import the following packages:


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

All servlets classes must extend the HttpServlet class

Servlets classes must provide and implementation for doGet, doPost,


ProcessRequest, init, destroy methods
Email List Application
HTML Code
<html>
<head>
<title> Email List application</title>
</head>
<body>
<h1>Join our email list</h1>
<p>To join our email list, enter your name and
email address below. <br>
Then, click on the Submit button.</p>

<form action="show_email_entry.jsp" method="get">


<table cellspacing="5" border="0">
<tr>
<td align="right">First name:</td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td align="right">Last name:</td>
<td><input type="text" name="lastName"></td>
</tr>
<tr>
<td align="right">Email address:</td>
<td><input type="text" name="emailAddress"></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
JSP Code <html>
<head>
<title>Email List application</title>
</head>
<body>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
%>
<h1>Thanks for joining our email list</h1>
<p>Here is the information that you entered:</p>

<table cellspacing="5" cellpadding="5" border="1">


<tr>
<td align="right">First name:</td>
<td><%out.println(firstName);%></td>
</tr>
<tr>
<td align="right">Last name:</td>
<td><%= lastName %></td>
</tr>
<tr>
<td align="right">Email address:</td>
<td><%= emailAddress %></td>
</tr>
</table>

<p>To enter another email address, click on the Back <br>


button in your browser or the Return button shown <br>
below.</p>

<form action=“index.htm" method="post">


<input type="submit" value="Return">
</form>
</body>
</html>
Servlet code import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddToEmailListServlet extends HttpServlet {


protected void DoGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
out.println(
"<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
+ "<html>\n"
+ "<head>\n"
+ " <title>Murach's Java Servlets and JSP</title>\n"
+ "</head>\n"
+ "<body>\n"
Servlet code + "<h1>Thanks for joining our email list</h1>\n"
+ "<p>Here is the information that you entered:</p>\n"
+ " <table cellspacing=\"5\" cellpadding=\"5\" border=\"1\">\n"
+ " <tr><td align=\"right\">First name:</td>\n"
+" <td>" + firstName + "</td>\n"
+ " </tr>\n"
+ " <tr><td align=\"right\">Last name:</td>\n"
+" <td>" + lastName + "</td>\n"
+ " </tr>\n"
+ " <tr><td align=\"right\">Email address:</td>\n"
+" <td>" + emailAddress + "</td>\n"
+ " </tr>\n"
+ " </table>\n"
+ "<br>\n"
+ "<p>To enter another email address, click on the Back <br>\n"
+ "button in your browser or the Return button shown <br>\n"
+ "below.</p>\n"
+ "</body>\n"
+ "</html>\n");
out.println("</html>");
} finally {
out.close();
}
}
Servlet Life Cycle
When a servlet is FIRST requested, it is loaded into the
servlet engine. The init() method of the servlet is invoked so
that the servlet may initialize itself.

Once initialization is complete, the request is then forwarded


to the appropriate method (ie. doGet or doPost)

The servlet is then held in memory. Subsequent requests are


simply forwarded to the servlet object.

When the engine wishes to remove the servlet, its destroy()


method is invoked.
How to Transit from a Page to Another

• Using of RequestDispatcher

RequestDispatcher rd=request.getRequestDispatcher(“x.jsp");
rd.forward(request, response);

• Using of Redirection of response

response.sendRedirect(“x.jsp");

You might also like