Professional Documents
Culture Documents
Perl N
DB
DB
Browser 1
DB
1. Java Servlet Architecture
Request – Response Process
Servlet container (servlet engine)
– Server that executes a servlet
Initialization
init()
Service
service()
doGet() Concurrent
doPost() threads
doDelete() of Execution
doHead()
doTrace()
doOptions()
Destruction
destroy()
Packages
javax.servlet
javax.servlet.http
javax.servlet.jsp
• A servlet is any class that implements the
javax.servlet.Servlet interface
– In practice, most servlets extend the
javax.servlet.http.HttpServlet class
– Some servlets extend
javax.servlet.GenericServlet instead
• Servlets, like applets, usually lack a main
method, but must implement or override
certain other methods
Generic Servlet & HTTP Servlet
GenericServlet
Client
request
Server service ( )
response
HTTPServlet
Browser
request doGet ( )
HTTP service ( )
Server
response doPost ( )
GenericServlet - Methods
• void init(ServletConfig config)
– Initializes the servlet.
• void destroy()
– Cleans up whatever resources are being held (e.g., memory, file
handlers, threads) and makes sure that any persistent state is
synchronized with the servlet's current in-memory state.
• ServletConfig getServletConfig()
– Returns a servlet config object, which contains any initialization
parameters and startup configuration for this servlet.
• String getServletInfo()
– Returns a string containing information about the servlet, such as
its author, version, and copyright.
Servlet Request Objects
• provides client request information to a servlet.
• the servlet container creates a servlet request object and
passes it as an argument to the servlet's service method.
• the ServletRequest interface define methods to retrieve
data sent as client request:
–parameter name and values
– attributes
– input stream
• HTTPServletRequest extends the ServletRequest
interface to provide request information for HTTP
servlets.
HttpServlet - Methods
void doGet (HttpServletRequest request,
HttpServletResponse response)
handles GET requests
void doPost (HttpServletRequest request,
HttpServletResponse response)
handles POST requests
void doPut (HttpServletRequest request,
HttpServletResponse response)
handles PUT requests
void doDelete (HttpServletRequest request,
HttpServletResponse response)
handles DELETE requests
HttpServletRequest - Methods
Enumeration getParameterNames()
an Enumeration of String objects, each String
containing the name of a request parameter; or an
empty Enumeration if the request has no
parameters
java.lang.String[] getParameterValues (java.lang.String name)
Returns an array of String objects containing all of
the values the given request parameter has, or null if
the parameter does not exist.
java.lang.String getParameter (java.lang.String name)
Returns the value of a request parameter as a String,
or null if the parameter does not exist.
HttpServletRequest - Methods
Cookie[] getCookies()
Returns an array containing all of the Cookie objects
the client sent with this request.
java.lang.String getMethod()
Returns the name of the HTTP method with which the
request was made, for example, GET, POST, or PUT.
java.lang.String getQueryString()
Returns the query string that is contained in the
request URL after the path.
HttpSession getSession()
Returns the current session associated with this
request, or if the request does not have a session,
creates one.
Servlet Response Objects
• Defines an object to assist a servlet in
sending a response to the client.
java.io.PrintWriter getWriter()
Returns a PrintWriter object that can send
character text to the client
Ex:
//Type I
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
2. Define the connection URL – specifies the server host,
port, and database name.
3. Establish the connection - a network connection to the
database can be established.
Syntax:
String url = "jdbc:odbc:" + dbName;
Ex:
Connection conn = DriverManager.getConnection ("jdbc:odbc:tour");
or
Connection conn = DriverManager.getConnection (“url");
4. Create a Statement object - to send queries and
commands to the database.
Ex:
Statement stmt = conn.createStatement ();
<servlet-mapping>
<servlet-name>Database</servlet-name>
<url-pattern>/DBAccess</url-pattern>
</servlet-mapping>
6. Form GET and POST actions
GET vs. POST
• Advantages of POST
– URL is simpler
– Data is hidden from people looking over user data
– Larger amounts of data can be sent
– Can send special characters (e.g., in uploaded files)
– Browsers will not cache results
– Should always be used if the requests changes data on
server
• Advantages of GET
– Can bookmark results page
– Browsers can cache results
– Easier to test interactively
Reading Form Data using Servlet:
• getParameter(): You call request.getParameter() method
to get the value of a form parameter.
• getParameterValues(): Call this method if the parameter
appears more than once and returns multiple values, for
example checkbox.
• getParameterNames(): Call this method if you want a
complete list of all parameters in the current request.
//getmethod.html
<html> <body>
<form action=“Sample" method="GET">
First Name: <input type="text" name="first_name"> <br />
Last Name: <input type="text" name="last_name"/>
<input type="submit" value="Submit" />
</form>
</body> </html>
//Sample.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
public class Sample extends HttpServlet
{ public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n"; out.println(docType + "<html>\n" +
"<head><title>" +
title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" + " <li><b>First
Name</b>: " + request.getParameter("first_name") + "\n" + "
<li><b>Last Name</b>: " + request.getParameter("last_name") +
"\n" + "</ul>\n" + "</body></html>");
}}
Program2: (POST method)
//testpost.html
<html> <body>
<form action=“Demo" method="POST">
First Name: <input type="text" name="first_name"> <br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body> </html>
//Demo.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
public class Demo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{response.setContentType("text/html"); PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" +
title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" + " <li><b>First Name</b>:
" + request.getParameter("first_name") + "\n" + " <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" + "</ul>\n" + "</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}}