Handling Http Request & Responses(doGet)

Estudies4you

Example 5:
Handling Http Request & Responses(doGet)

·         This example uses doGet method
·         Create following files in a folder (Please check next slide for code)
o   HTMLFile index.html Contains html that allows user to enter details
o   Java File HTTPReqResPost.java Contains logic to get the params details
o   DD web.xml Contains deployment details. Deployment Descriptor
·         Compile the Java File as you did in earlier example
·         Create below structure with files that you created above
o   Folder "HTTPReqResGet" contains
o   I__"WEB-INF" Folder and index.html
o   I__"WEB-INF" Folder contains "classes" Folder and web.xml File
o   I__"classes" Folder contains "HTTPReqResGet.class" File
·         Deploy the HTTPReqResGet folder inside webapps folder of Tomcat
·         Bounce the Server
·         Send request using "http://localhost:8550/HTTPReqResGet/index.html"
index.html
<html>
<body>
 <center>
<h1 align="center">Welcome to Student Registration Page...</h1>
<form name=”StudentForm” action=”StudentGet.do”>
<table>
<tr>
<td><B>Name of Student : </td>
<td><input type=textbox name="StudentNameParam" size="25" value=""></td>
</tr>    
<tr>
<td><B>Registration # </td>
<td><input type=textbox name="RollNumberParam" size="25" value=""></td>
</tr>
<tr>
<td><B>Father's Name : </td>
<td><input type=textbox name="FatherName" size="25" value=""></td>
</tr>
<tr>
<td><B>Gender : <input type="radio" name="gender" value="male">Male &nbsp; &nbsp; <input type="radio" name="gender” value=”female”> </td>
</tr>
</table> <input type=submit value="Submir>
</form>
</body>
</html>

·         HTTPReqResGet.java: This prepares the response as HTML file that displays value of request parameters that are provided in servlet.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTTPReqResGet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("StudentNameParam");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.printIn("<B>Entered Student name is : ");
pw.println(name);
pw.close();
}
                }
·         web.xml
o   <servlet> tag maps your servlet class with internal name
o   <servlet-mapping> tag maps internal name with url-pattern that your invoke
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>getservletget</servlet-name>
<servlet-class>HTTPReqResGet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getservletget</servlet-name>
<url-pattern>/StudentGet.do</url-pattern>
</servlet-mapping>
</web-app>
javax.servlet.http Package,Interfaces of javax.servlet.http package,Classes of javax.servlet.http package,Method of HttpServletRequest Interface,Methods of HttpServletResponse Interface,Methods of HttpSession Interface,Methods of HttpSessionBindingListener Interface,Reading Initialization Parameters,Reading Servlet Parameters,Methods of ServletRequest Interface,Methods of ServletResponse Interface,ServletResponse Interface,ServletRequest Interface,Methods of Servlet Interface,Methods of ServletConfig Interface,Methods of ServletContext Interface,ServletContext Interface,ServletConfig Interface,Servlet Interface,How to install Tomcat,How to install Tomcat webserver,Instructions to install and configure the Tomcat,steps to install tomcat webserver,process to install tomcat webserver,tomcat webserver installation,tomcat webserver installation steps,tomcat webserver installation steps,estudies4you,Web Technologies,JNTUH R16 Web Technologies Lecture notes pdf,Web Technologies class room notes pdf,Web Technologies notes unit wise,Web Technologies course file,Web Technologies previous questio papers,Web Technologies old question papers,Web Technologies MCQ,jntuh r16 Web Technologies syllabus pdf,Web Technologies study material,examples of tomcat server,example of tomcat server with program,Servlet that checks user details,Interfaces of javax.servlet package,javax.Servlet Package,API to develop Servlet,Handling Http Request & Responses

To Top