1. An Introduction to Java Web Technology (Java Servlet) Presented At: JECRC, Faculty of E&T, Jodhpur National University, Jodhpur, Rajasthan India -342001
2.
3. HTTP METHODS Uploads a representation of the specified resource. PUT Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy. [3] CONNECT Returns the HTTP methods that the server supports for specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource. OPTIONS Echoes back the received request, so that a client can see what intermediate servers are adding or changing in the request. TRACE Deletes the specified resource. DELETE Submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. POST means retrieve whatever data is identified by the URI GET Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content. HEAD DESCRIPTION METHOD
4. HTTP METHODS HTTP servers are required to implement at least the GET and HEAD methods [4] and, whenever possible, also the OPTIONS method Safe methods Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as safe , which means they are intended only for information retrieval and should not change the state of the server. Idempotent methods Some http methods are defined to be idempotent, meaning that multiple identical requests should have the same effect as a single request. Methods PUT, DELETE, GET, HEAD, OPTIONS and TRACE, being prescribed as safe, should also be idempotent. HTTP is a stateless protocol. By contrast, the POST method is not necessarily idempotent, and therefore sending an identical POST request multiple times may further affect state or cause further side effects.(like updating inserting records in database multiple times).
17. SETTING UP SERVLET DEVELOPMENT ENVIRONMENT In the variable name filed, enter JAVA_HOME. Specify the path to root directory of JAVA installation in variable value field and click OK button. Now JAVA_HOME will appear under user variables. Next you need to add bin directory under the root directory of JAVA installation in PATH environment variable.Select the PATH variable from System variables and click on Edit button. Add: ;%JAVA_HOME%in; at the end of variable value field and click OK button.
18. INSTALLING TOMCAT Tomcat is an opensource web container. it is also web container reference implementation. Download the latest version of tomcat from this URL . Download the jakarta-tomcat-5.0.28.tar.gz and extract it to the directory of your choice. Note: This directory is referred as TOMCAT_HOME in other tutorials. That’s all, tomcat is installed. Starting and shutting down Tomcat To start the tomcat server, open the command prompt, change the directory to TOMCAT HOME/bin and run the startup.bat file. It will start the server. >startup To shut down the tomcat server, run the shutdown.bat file. It will stop the server. >shutdown Verifying Tomcat installation To verify that tomcat is installed properly, start the server as explained above, open the web browser and access the following URL. http://localhost:8080/index.jsp It should show the tomcat welcome page, if tomcat is installed properly and server is running.
19. INSTALLING TOMCAT Setting up the CLASSPATH Now you need to create a new environment variable CLASSPATH if it is not already set. We need to add the servlet-api.jar into the CLASSPATH to compile the Servlets. Follow the same steps as you did to create the JAVA_HOME variable. Create a new variable CLASSPATH under system variables. Add TOMCAT_HOME/lib/servlet-api.jar in variable value field. Note: here TOMCAT_HOME refers to the tomcat installation directory.
53. SIMPLE COUNTING IN SERVLET import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SimpleCounter extends HttpServlet{ int counter = 0; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); counter++; pw.println("At present the value of the counter is " + counter); } }
54. GETTING SERVER INFORMATION import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SnoopingServerServlet extends HttpServlet{ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); pw.println("The server name is " + request.getServerName() + "<br>"); pw.println("The server port number is " + request.getServerPort()+ "<br>"); pw.println("The protocol is " + request.getProtocol()+ "<br>"); pw.println("The scheme used is " + request.getScheme()); } }
57. READING INITIALIZATION PARAMETER In this example we are going to retreive the init paramater values which we have given in the web.xml file. Whenever the container makes a servlet it always reads it deployment descriptor file i.e. web.xml. Container creates name/value pairs for the ServletConfig object. Once the parameters are in ServletConfig they will never be read again by the Container. The main job of the ServletConfig object is to give the init parameters.
58. READING INITIALIZATION PARAMETER import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class InitServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); pw.print("Init Parameters are : "); Enumeration enumeration = getServletConfig().getInitParameterNames(); while (enumeration.hasMoreElements()){ pw.print(enumeration.nextElement() + " "); } pw.println("The email address is " + getServletConfig().getInitParameter("AdminEmail")); pw.println("The address is " + getServletConfig().getInitParameter("Address")); pw.println("The phone no is " + getServletConfig().getInitParameter("PhoneNo")); } }
61. PASSING PARAMETER USING HTML FORM <html> <head> <title>New Page 1</title> </head> <body> <h2>Login</h2> <p>Please enter your username and password</p> <form method="GET" action="/htmlform/LoginServlet"> <p> Username <input type="text" name="username" size="20"></p> <p> Password <input type="text" name="password" size="20"></p> <p><input type="submit" value="Submit" name="B1"></p> </form> <p> </p> </body> </html>
62. PASSING PARAMETER USING HTML FORM import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name = request.getParameter("username"); String pass = request.getParameter("password"); out.println("<html>"); out.println("<body>"); out.println("Thanks Mr." + " " + name + " " + "for visiting roseindia<br>" ); out.println("Now you can see your password : " + " " + pass + "<br>"); out.println("</body></html>"); } }
63. PASSING PARAMETER USING HTML FORM <?xml version="1.0" encoding="ISO-8859-1"?> <!--<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> --> <web-app> <servlet> <servlet-name>Hello</servlet-name> <servlet- class >LoginServlet</servlet- class > </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> </web-app>
65. MULTIPLE VALUES FOR A SINGLE PARAMETER <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Insert title here</title> </head> <body> <form method = "post" action = "/GetParameterServlet/GetParameterValues"> <p>Which of the whisky you like most</p> <input type = "checkbox" name ="whisky" value = "RoyalChallenge">RoyalChallenge.<br> <input type = "checkbox" name ="whisky" value = "RoyalStag">RoyalStag.<br> <input type = "checkbox" name ="whisky" value = "Bagpiper">Bagpiper.<br> <input type ="submit" name= "submit"> </form> </body> </html>