Home | » | Java | » | Advance Java | » | Java Servlets | » | Creating Java Servlets |
---|
A Servlet is like any other Java program - Basically a class.
To create a Servlet class, that class must be subclassed either form the GenericServlet class or some other subclass of the GenericServlet class.
Servlets are more often that not used to provide dynamic content on the World Wide Web. The Servlet API consists of certain specialized classes and interfaces, which provide users with the necessary properties and methods to implement servlets on the Web.
The HttpServlet Abstract Class
The HttpServlet class is an abstract class that simplifies writing HTTP serlvets. It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. Because it is an abstract class, servlet writers must subclass it and override at least one method.
The methods normally overriden are:
The lifecycle methods init() and destroy() can also be overriden if the servlet is managing resources that are held for the lifetime of the servlet. Servlets that do not manage resources do not need to spcialize these methods.
The service() method is not typically overriden. The service() method, as provided supports standard HTTP requests by dispatching them to appropriate methods, such as the methods listed above that have the prefix "do".
The service() method
Declaration
Parameters
HttpServletRequest - This parameter encapsulates the HTTP request to the servlet.
HttpServletResponse - This parameter encapsulates the servlet's response to an HTTP.
The HttpServletRequest Interface
The HttpServletRequest interface encapsulates information of the request made to a servlet. This interface gets information from the client to the servlet for use in the service() method. It allows HTTP protocol specified header information to be accessed from the service() method.
HttpServletRequest Methods
getMethod() | Returns the HTTP method (GET, POST) with which the request was made. |
getQueryString() | Returns any Query String that is part of the HTTP request |
getRemoteUser() | Returns the name of the user making the HTTP request |
getRequestedSessionId() | Returns the Session Id specified with the HTTP request |
getSession(boolean) | If boolean is FALSE, it returns the current valid session associated witht the HTTP request. If boolean is TRUE it creates a new session |
IsRequestSessionIdValid() | Checks whether the HTTP request is associated with a session that is valid in the current session context. |
getCookies() | Returns the array of cookies found in the HTTP request. |
The HttpServletResponse Interface
The HttpServletResponse interface encapsulates information of the response sent by a servlet to a client. This interface allows a servlet's service() method to manipulate HTTP protocol specified header information and return data to its client.
HttpServletResponse Methods
addCookie(Cookie) | Adds the specified cookie to the response |
encodeUrl(String) | Encodes the specified URL by adding the session id to it. If encoding it not required, it returns the URL unchanged. |
sendRedirect(String) | Sends a temporary redirect response to a client using the specified redirect location URL. |
sendError(int) | Sends an error response to a client using the specified status code and a default message. |
Creating a Java Servlet
To write a Java servlet, you need to import the javax.servlet.* package of Java. The Java servlet are executed on the Web Server. The following program code creates a servlet that prints a text message on the Web browser using the println() method of Java servlet.
Compiling and Running a Java Servlet
Servlet are compiled using the javax.servlet and javax.servlet.http package of Java. If the compiler does not have these packages, then yo neeed install Java Servlet Development Kit (JSDK) and include the JSDK classes in the CLASSPATH to compile the servlet.
To compile and run the java servlet first of all you need to install any java development kit and then web server like Apache Tomcat 4.0 after installing both now copy javax folder and then paste it into bin dir - "c:/jdk1.3/bin/"
Installing Apache Tomcat 4.0
|
|
|
|
|
|
|
|
Download - Java
Download - Apache Tomcat 4.0
more » Apache Tomcat Versions
Download - javax
After install Apache Tomcat 4.0 now Start it from Diagram 24.10 or...
"Start -> All Programs -> Apache Tomcat 4.0 -> Start Tomcat." and minimize window
Now compile Java Servlet Program using below command as shown diagram 24.11
|
Diagram 24.11 Compile Java Servlet |
Now, copy "SimpleServlet.class" File and then paste it under "C:/Program Files/Apache Tomcat 4.0/webapps/examples/WEB-INF/classes/" |
|
Diagram 24.11 Copy SimpleServlet.class file under classes dir |
The above code creates a sample Java Servlet that displays a text message on the Web browser.
Diagram 24.12 shows the Web browser window a text message displayed on it.
|
Diagram 24.12 Output of Running a Java Servlet |
Java Servlet Programming
- Creating Java Servlets (Compiling and Running a Java Servlet)
- Generic and Http Servlets
- Calling a servlet from within an HTML page
- Using JDBC In Servlet
- User Authentication Form using Java Servlet
No comments:
Post a Comment