0% found this document useful (0 votes)
9 views7 pages

GET&POST

The document outlines the differences between GenericServlet and HttpServlet classes, detailing their functionalities and use cases. It also compares GET and POST request methods, highlighting their characteristics and security implications. Additionally, it explains the differences between ServletConfig and ServletContext interfaces and provides steps to create a servlet application, including directory structure, servlet creation, compilation, deployment descriptor, and server startup.

Uploaded by

vijaya51good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

GET&POST

The document outlines the differences between GenericServlet and HttpServlet classes, detailing their functionalities and use cases. It also compares GET and POST request methods, highlighting their characteristics and security implications. Additionally, it explains the differences between ServletConfig and ServletContext interfaces and provides steps to create a servlet application, including directory structure, servlet creation, compilation, deployment descriptor, and server startup.

Uploaded by

vijaya51good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Difference between GenericServlet class and HttpServlet class

S.No Generic Servlet Http Servlet

1 It is a class available in javax.servlet.*. It is a class available in javax.servlet.http.*


It is a abstract class. It doesnot contain abstract methods.
2 Generic Servlet class implements Servlet HttpServlet class extends Generic Servlet class.
and ServletConfig interfaces.
3 It provides the definition for all the No need to override service() method.
methods available in Servlet interface
except service() method. It Override the
service().
4 Intit(),service(),destroy(), getServletInfo(), Inaddition to all the methods of Generic Servlet ,It has
getServletConfig(), getInitParameter(), doGet(),doPost(),doPut()
getInitParameterNames(), Note:doGet()used for Select operations.
getServletContext(). doPost()-used for DML commands.

5 It supports Protocol independent It supports protocol dependent implementation.


implementation.
6 It can handle any type of request. It handles only Http request.
(FTP,HTTP,SMTP,TELNET)
It doesn’t provide complete
implementation for the HTTP.
7 It can’t implement Cookies, Session It implements Cookies, Session tracking mechanism.
tracking mechanism.
8 Intit(),service(),destroy(), getServletInfo(), Inaddition to all the methods of Generic Servlet ,It has
getServletConfig(), getInitParameter(), doGet(),doPost(),doPut()
getInitParameterNames(), Note:doGet()used for Select operations.
getServletContext(). doPost()-used for DML commands.
HTTP Methods: GET,POST, HEAD, OPTIONS, PUT, DELETE, TRACE

2.Difference between get request and post request methods

S. No Get Request Post Request


1 Get method is used to get the information from Post method is used to post the
the server. information to the server.
2 Usually GET requests are READ-ONLY. Usually POST requests are WRITE or
UPDATE Operations.
3 End user provided information will be append to End user provided information will be
the URL as the part of Query String and send to encapsulated in the request body and send
the server. to the server.
4 By using get request , we can send only By using post request , we can send both
character data and we can’t send binary data binary and character data to the server.
like images.
5 We can send only limited amount of information We can send huge amount of information
which is varied from browser to browser. to the server.
6 Security is less and hence we can’t send Security is more and hence we can send
sensitive information like username and sensitive information like username and
password. password.
7 Bookmarking of get request is possible. Bookmarking of post request is not
possible.
8 Caching of get request is possible. Caching of post request is not possible.
9 Get request is safe. Post request is not safe.
10 There are multiple ways to send get request. There is only one way to send post request.
11 a. Typing URL in the address bar and enter Submitting the html form with method
b. Clicking hyperlink attribute of POST value.
c. Submitting the html form without method
attribute.
d. Submitting the html form with method
attribute of GET value.
3.Differences between ServletConfig interface and ServletContext interface.

S.N ServletConfig ServletContext


o
1 It is a interface available in javax.servlet.*. It is also a interface available in javax.servlet.*
It is implemented by GenericServlet class.
2 For every Servlet, WebContainer creates on For every Webapplication, WebContainer creates on context object
config object. to hold application level configuration information.
3 Config object will be created at the time of context object will be created at the time of Application deployment
servlet object creation and destroyed at the time and destroyed at the time of Application undeployment.
of servlet object destruction.
4 Within Servlet, config object will be accessed by Within Servlet, context object will be accessed by using
using ServletContext context=getServletContext()
ServletConfig config=getServletConfig()
5 This config object is used to get the Servlet level This context object is used to get the Application level configuration
configuration information information
6 It is not object of javax.servlet.ServletConfig(). It is not object of javax.servlet.ServletContext().
It is the object of it’s implementation class It is the object of it’s implementation class provided by WebServer
provided by WebServer Vendor. Vendor.
7 By Using config object , servlet can get it’s By using context object, we can call
Configuration information like logical name of getRequestDispatcher(),getInitParameter(),getAttribute(),
the servlet,initialization parameters by using setAttribute(),getServletInfo(),getResourcePaths(),
below methods, getContextPath(),log(),getMajorVersion(),getMinorVersion().
getServletName(),getInitParameter(),getInit
ParameterNames(),getServletContext().
8 Uses <init-param> to specify the servlet level Uses <context-param> to specify the initialization parameter
initialization parameter values. values.
9 Inside the web.xml file, Inside the web.xml file,
<web-app> <web-app>
<servlet> <servlet> </servlet>
<init-param> <conext-param>
<param-name>username</param-name> <param-name>username</param-name>
<param-value>Mecs</param-value> <param-value>Mecs</param-value>
</init-param> </context-param>
</servlet> </web-app>
</web-app>
10 It is servlet specific. It is application specific.
Steps required to create Servlet Application

1. Create directory structure for your application.


2. Create a Servlet
3. Compile the Servlet
4. Create Deployement Descriptor for your application
5. Deploy the application and Start the server

lets create our first Servlet Application.

1. Creating the Directory Structure: Sun Microsystem defines a


unique directory structure that must be followed to create a servlet
application.

Create the above directory structure inside Apache-Tomcat\


webapps directory. All HTML, static files(images, css etc) are kept directly
under Web application folder. While all the Servlet classes are kept
inside classes folder.

The web.xml (deployement descriptor) file is kept under WEB-INF folder.

2. Creating a Servlet: There are three different ways to create a servlet.

 By implementing Servlet interface


 By extending GenericServlet class
 By extending HttpServlet class
But mostly a servlet is created by extending HttpServlet abstract class. As
discussed earlier HttpServlet gives the definition of service() method of
the Servlet interface. The servlet class that we will create should not
override service() method. Our servlet class will override
only doGet() or doPost() method.

When a request comes in for the servlet, the Web Container calls the
servlet's service() method and depending on the type of request
the service() method calls either the doGet() or doPost() method.

NOTE: By default a request is Get request.

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

public class MyServlet extends HttpServlet

public void doGet(HttpServletRequest request,HttpServletResponse response)throws


ServletException,IOException

response.setContentType(“html/text”);

PrintWriter out=response.getWriter();

out.println(“<html><body>”);

out.println(“<h1>HelloServlet</h1>”);

out.println(“</body></html>”);

Copy
Write above code in a notepad file and save it as MyServlet.java anywhere
on your PC. Compile it(explained in next step) from there and paste the class
file into WEB-INF/classes/ directory that you have to create
inside Tomcat/webapps directory.

3. Compiling a Servlet: To compile a Servlet a JAR file is required.


Different servers require different JAR files. In Apache Tomcat server servlet-
api.jar file is required to compile a servlet class.

Steps to compile a Servlet:

 Set the Class Path.


 Download servlet-api.jar file.
 Paste the servlet-api.jar file inside Java\jdk\jre\lib\ext directory.

 Compile the Servlet class.


NOTE: After compiling your Servlet class you will have to paste the class file
into WEB-INF/classes/ directory.

4. Create Deployment Descriptor: Deployment Descriptor(DD) is


an XML document that is used by Web Container to run Servlets and JSP
pages. DD is used for several important purposes such as:

 Mapping URL to Servlet class.


 Initializing parameters.
 Defining Error page.
 Security roles.
 Declaring tag libraries.
we will see how to create a simple web.xml file for our web application.

Web.xml file

<web-app>
<servlet>
<servlet-name>MECS</servlet-name>
<servlet-class> MyServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MECS</servlet-name>
<url-pattern>/ADD</url-pattern>
</servlet-mapping>
</web-app>
Note:<servlet-name> specifies logical name of the servlet

<servlet-class> specifies the name of the class.

<url-pattern> specifies the URL value of the action attribute in <form>


element in HTML.

5. Start the Server: Double click on the startup.bat file to start your
Apache Tomcat Server.

Or, execute the following command on your windows machine using RUN
prompt.

C:\apache-tomcat-7.0.14\bin\startup.bat
6. Starting Tomcat Server for the first time: If you are starting
Tomcat Server for the first time you need to set JAVA_HOME in the
Enviroment variable. The following steps will show you how to set it.

 Right Click on My Computer, go to Properites.


 Go to Advanced Tab and Click on Enviroment Variables... button.

 Click on New button, and enter JAVA_HOME inside Variable name text
field and path of JDK inside Variable value text field. Click OK to save.

7. Run Servlet Application

Open Browser and type http:localhost:8080/First/hello

You might also like