Servlets: Complete Reference To Java - Herbertschildt
Servlets: Complete Reference To Java - Herbertschildt
Servlets: Complete Reference To Java - Herbertschildt
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet {
public void service(ServletRequest request,
ServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Hello!");
pw.close();
}
}
A Simple Servlet
Interface
Classes
The Servlet Interface
Two files
ColorGet.html - A web page
ColorGetServlet.java - a servlet
<html>
<body>
<center>
<form name="Form1"
action="http://localhost:8080/servlets-examples/servlet/ColorGetServlet">
<B>Color:</B>
<select name="color" size="1">
<option value="Red">Red</option> • It defines a form that
<option value="Green">Green</option> contains a select element
<option value="Blue">Blue</option> and a submit button.
</select> • Notice that the action
<br><br>
<input type=submit value="Submit">
parameter of the form tag
</form> specifies a URL.
</body> • The URL identifies a servlet
</html> to process the HTTP GET
request.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; • The doGet( ) method is
public class ColorGetServlet extends HttpServlet { overridden to process any
public void doGet(HttpServletRequest request, HTTP GET requests that
HttpServletResponse response) are sent to this servlet.
throws ServletException, IOException { • It uses the
String color = request.getParameter("color"); getParameter() method
of HttpServletRequest
response.setContentType("text/html");
to obtain the selection
PrintWriter pw = response.getWriter();
that was made by the
pw.println("<B>The selected color is: ");
user.
pw.println(color); • A response is then
pw.close(); formulated.
}
}
Handling HTTP POST Requests
Two files
ColorPost.html - A web page
ColorPostServlet.java - a servlet
<html>
<body>
<center>
<form name="Form1"
method="post"
action="http://localhost:8080/servlets-examples/servlet/ColorPostServlet">
<B>Color:</B>
<select name="color" size="1"> • It is identical to ColorGet.htm except
<option value="Red">Red</option>
<option value="Green">Green</option> that the method parameter for the form
<option value="Blue">Blue</option> tag explicitly specifies that the POST
</select> method should be used, and the action
<br><br> parameter for the form tag specifies a
<input type=submit value="Submit"> different servlet.
</form>
</body>
</html>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColorPostServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response) • The doPost( ) method is
throws ServletException, IOException { overridden to process any
String color = request.getParameter("color");
HTTP POST requests that are
response.setContentType("text/html"); sent to this servlet.
PrintWriter pw = response.getWriter(); • It uses the getParameter( )
pw.println("<B>The selected color is: "); method of
HttpServletRequest to
pw.println(color); obtain the selection that was
pw.close(); made
} • by the user.
} • A response is then formulated.
Using Cookies