Home | Servlet Basics

Chapter: Web or internet Programming : Applets

Servlet Basics

The Servlet API consists of two packages, javax.servlet, and javax.servlet.http.

Servlet Basics

 

 

The Servlet API consists of two packages, javax.servlet, and javax.servlet.http.

 

The javax is there because servlets are a standard extension to Java, rather than a mandatory part of the API. Thus JVM developers are not required to include classes for them in their Java development and execution environments. Sun has kept the distribution of the servlets API separate from the Java 2 platform because the Servlet API is evolving much faster than the core Java SDK. You can find the Java Servlet Development Kit (JSDK) at http://java.sun.com/products/servlet/.  The JSDK includes the necessary servlet classes and a small servletrunner application for development and testing.

HTTP Servlets

 

The HttpServlet class is an extension of GenericServlet class that includes methods for handling HTTP specific data. HttpServlet defines a number of methods, such as doGet(), and doPost(), to handle particular types of HTTP requests (GET, POST, etc.). These methods are called by the default implementation of the service() method, which figures out the kind of request being made and then invokes the appropriate method. (service() is defined in

 

GenericServlet class).

 

 

Example 1:

 

 

import javax.servlet.*; import javax.servlet.http.*; import java.io.*;

 

 

public class HelloWorldServlet extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

 

resp.setContentType(“text/html”);

 

PrintWriter out = resp.getWriter();

 

 

out.println(“<HTML>”); out.println(“<HEAD><TITLE>Have you seen this

before?</TITLE></HEAD>”);

 

out.println(“<BODY><H1>Hello World!</H1></BODY></HTML>”);

 

}

 

}

 

 

The doGet() method is called whenever anyone requests a URL that points to this servlet.

 

The servlet is installed in the servlets directory and its URL is http://site:8080/servlet/HelloWorldServlet. The doGet() method is actually called by the default service() method of HttpServlet. The service() method is called by the web server when a request is made of HelloWorldServlet; the method determines what kind of HTTP request is being made and dispatches the request to the appropriate doXXX() method (in this case, doGet()). doGet() is passed two objects, HttpServletRequest ,and HttpServletResponse, that contain information about the request and provide a mechanism for the servlet to provide a response, respectively.

 

 

Example 2:

 

This example deals with forms and dynamic HTML.

 

 

The HTML form that calls the servlet using a GET request is as follows:

 

 

<HTML>

 

<HEAD><TITLE> Greetings Form</TITLE></HEAD> <BODY>

 

<FORM METHOD=GET   ACTION=”/servlet/HelloServlet”>

 

What is your name?

 

<INPUT TYPE=TEXT       NAME=username SIZE=20>

 

<INPUT TYPE=SUBMIT  VALUE=”Introduce Yourself”>

 

</FORM>

 

</BODY>

 

</HTML>

 

 

The form submits a variable named username to the URL /servlet/HelloServlet. The servlet is as follows:

 

 

import javax.servlet.*; import javax.servlet.http.*; import java.io.*;

 

public class HelloServlet extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

 

resp.setContentType(“text/html”);

 

PrintWriter out = resp.getWriter();

 

 

out.println(“<HTML>”);

 

out.println(“<HEAD><TITLE>Finally, interaction!</TITLE></HEAD>”); out.println(“<BODY><H1>Hello,” + req.getParameter(“username”) +

“!</H1>” );

 

out.println(</BODY></HTML>”);

 

}

 

}

 

 

The getParameter() method of HttpServletRequest is used to retrieve the value of the form variable. When a server calls a servlet, it can also pass a set of request parameters.

 

 

 

The POST request

 

 

The POST request is designed for posting information to the server, although in practice it is also used for long parameterized requests and larger forms, to get around limitations on the length of URLs. The doPost() method is the corresponding method for POST requests. If your servlet is performing database updates, charging a credit card, or doing anything that takes an explicit client action, you should make sure that this activity is happening in a doPost() method.

 

This is because POST requests are not idempotent, which means that they are not safely repeatable, and web browsers treat them specially. E.g. a browser cannot bookmark them. GET requests are idempotent, so they can safely be bookmarked, and a browser is free to issue the request repeatedly (say to get some information from the web server) without necessarily consulting the user. Hence it is not appropriate to charge a credit card in a GET method!

 

To create a servlet that can handle POST requests, you need to override the default doPost() method from HttpServlet and implement the necessary functionality. If necessary, your application can implement different code in doPost() and doGet(). For instance, the doGet() method might display a postable data entry form that the doPost() method processes. doPost() can even call doGet() at the end to display the form again.

 

Servlet Responses

 

 

In the case of an Http servlet, the response can include three components: a status code, any number of HTTP headers, and a response body.

 

You use setContentType() method of the response object passed into the servlet to set the type of the response. Examples include “text/html” for text, “image/gif” for returning a GIF file from the database, and “application/pdf” for Adobe Acrobat files.

 

ServletResponse and HttpServletResponse each define two methods for producing output streams, getOutputStream() and getWriter(). The former returns a ServletOutputStream that can be used for text or binary data. The latter returns a java.io.PrintWriter object used for text data.

 

You can use setStatus() or sendError() method to specify the status code sent back to the server. Examples include, 200 (“OK”), 404 (“Not Found”) etc. The sendRedirect() method allows you to issue a page redirect. Calling this sets the Location header to the specified location and uses the appropriate status code for a redirect.

 

Servlet Requests

 

 

When a servlet is asked to handle a request, it typically needs specific information about the request so that it can process the request appropriately. For example, a servlet may need to find out about the actual user who is accessing the servlet, for authentication purposes.

 

ServletRequest and ServletRequest provide these methods. Examples include getProtocol() (protocol used by request), getRemoteHost() (client host name), getServerName()

 

(name of the web server), getServerPort() (port number the web server listens at), getParameter() (access to request parameters as form variables), and getParameterValues() (returns an array of strings that contains all the values for a particular parameter).

 

Example 3:

 

 

This servlet requests information to restrict access.

 

 

import javax.servlet.*; import javax.servlet.http.*; import java.io.*;

 

 

public class SecureRequestServlet extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

 

resp.setContentType(“text/html”);

 

PrintWriter out = resp.getWriter();

 

 

out.println(“<HTML>”); out.println(“<HEAD><TITLE>Semi-Secure Request</TITLE></HEAD>”);

 

out.println(“<BODY>”);

 

 

String remoteHost = req.getRemoteHost();

 

String scheme = req.getScheme();

 

String authType = req.getAuthType();

 

 

if((remoteHost == null) || (scheme == null) || (authType == null)) { out.println(“Request information was not available”);

 

return;

 

}

 

 

// look for a secure HTTP connection from a .gov host using Digest style / //authentication

 

if(scheme.equalsIgnoreCase(“https”) && remoteHost.endsWith(“.gov”) && authType.equals(“Digest”)) { out.println(“Special, secret information”);

 

}

 

else {

 

out.println(“You are not authorized to read this information”);

 

}

 

out.println(“</BODY></HTML>”);

 

}

 

}

 

 

Error Handling

 

 

If the error is part of a servlet’s normal operation, such as when a user forgets to fill in a required form field, then write an error message to the servlet’s output stream.

 

If the error is a standard HTTP error, use the sendError() method of HttpServletResponse to tell the server to send a standard error status code.

 

E.g. if a file was not found, resp.sendError(HttpServletResponse.SC_NOT_FOUND);

Example 4:

 

 

This servlet serves HTML files.

 

import javax.servlet.*;                                  import javax.servlet.http.*;  import java.io.*;

 

 

public class FileServlet extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

 

File r;

 

FileReader fr;

 

BufferedReader br;

 

 

try {

 

r = new File(req.getParameter(“filename”); fr = new FileReader(r);

br = new BufferedReader(fr);

 

 

if (!r.isFile()) { resp.sendError(resp.SC_NOT_FOUND); return;

}

 

}

 

catch (FileNotFoundException e) { resp.sendError(resp.SC_NOT_FOUND); return;

}

 

catch (SecurityException se) { //Be unavailable permanently throw(new UnavailableException(this, “Servlet lacks appropriate

 

privileges”));

 

}

 

 

resp.setContentType(“text/html”);

 

PrintWriter out = resp.getWriter();

 

String text;

 

while ((text = br.readLine()) != null) out.println(text);

 

br.close();

 

}

 

}

 

Servlet Initialization: When a server loads a servlet for the first time, it calls the servlet’s init() method. In its default implementation, init() handles some basic housekeeping, but a servlet can override the method to perform other tasks. This includes performing I/O intensive tasks such as opening a database connection. You can also create threads in init() to perform other tasks such as pinging other machines on the network to monitor the status of these machines. When an actual request occurs, the service methods can use the resources created in init(). The default init() is not a do-nothing method. You must call always call super.init() as the first action in your own init() routines.

 

The server passes the init() method a ServletConfig object. This object encapsulates the servlet initialization parameters, which are accessed via the getInitParameter() and getInitParameterNames() methods. GenericServlet and HttpServlet both implement the

 

ServletConfig interface, so these methods are always available in a servlet. Consult your server documentation on how to set the initialization parameters.

 

Each servlet also has a destroy() method that can be overwritten. This method is called when a server wants to unload a servlet. You can use this method to free important resources.

 

Example 5:

 

 

This is a persistent counter servlet that saves its state between server shutdowns. It uses the init() method to first try to load a default value from a servlet initialization parameter. Next the init() method tries to open a file named /data/counter.dat and read an integer from it. When the servlet is shut down, the destroy() method creates a new counter.dat file with the current hit-count for the servlet.

 

import javax.servlet.*;   import javax.servlet.http.*;     import java.io.*;  

public class LifeCycleServlet extends HttpServlet {               

int timesAccessed;                            

public void init(ServletConfig conf) throws ServletException {      

super.init(conf);                      

// Get initial value                   

try {                    

timesAccessed = Integer.parseInt(getInitParameter(“defaultStart”));          }

catch (NullPointerException e) {               

timesAccessed = 0;        }                

catch (NumberFormatException e) {                  

timesAccessed = 0;        }                

// Try loading from the disk                       

try {

 

File r = new File(“./data/counter.dat”);

 

DataInputStream ds = new DataInputStream(new FileInputStream(r));

 

timesAccessed = ds.readInt();         }

 

catch (FileNotFoundException e){

 

//Handle error               }

 

catch (IOException e) {

 

//Handle error               }

 

finally {

 

ds.close();          }   }

 

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

resp.setContentType(“text/html”);

 

PrintWriter out = resp.getWriter(); timesAccessed++; out.println(“<HTML>”);

 

out.println(“<HEAD><TITLE>Life Cycle Servlet</TITLE></HEAD>”); out.println(“<BODY>”);

 

out.println(“I have been accessed “ + timesAccessed + “time[s]”); out.println(“</BODY></HTML>”); }

 

public void destroy() {

 

// Write the integer to a file

 

File r = new File(“./data/counter.dat”); try {

 

DataOutputStream dout = new DataOutputStream(new FileOutputStream(r)); dout.writeInt(timesAccessed);

 

}

 

catch (IOException e) {

 

// Handle error, maybe log the error } finally ()

 

 

dout.close();

 

}  }   }

 

 

Example 6

 

This servlet implements an ATM display. The doGet() method displays the current account balance and provides a small ATM control panel for making deposits and withdrawals. The control panel uses a POST request to send the transaction back to the servlet, which performs the appropriate action and calls doGet() to redisplay the ATM screen with the updated balance. This example also demonstrates thread safety.

 

import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.io.*;

 

public class AtmServlet extends HttpServlet {

 

Account act;

 

public void init (ServletConfig conf) throws ServletException {

 

super.init();

 

act = new Account();

 

act.balance = 0;    }

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

 

resp.setContentType(“text/html”);

 

PrintWriter out = resp.getWriter(); out.println(“<HTML><BODY>”);

 

out.println(“<H2> First Bank of Java ATM</H2>”); out.println(“Current Balance: <B>” + act.balance + “</B><BR>”);

 

out.println(“<FORM METHOD=POST ACTION=/servlet/AtmServlet>”); out.println(“Amount: <INPUT TYPE=TEXT NAME=AMOUNT SIZE=3><BR>”);

 

out.println(“INPUT TYPE=SUBMIT NAME=DEPOSIT VALUE=\”Deposit\”>”); out.println(“INPUT TYPE=SUBMIT NAME=WITHDRAW VALUE=\”Withdraw\”>”); out.println(“</FORM>”);

 

out.println(“</BODY></HTML>”);

 

}

 

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

int amt = 0;

 

try {

 

amt = Integer.parseInt(req.getParameter(“AMOUNT”));

 

}

 

catch (NullPointerException e) {

 

// No amount Parameter passed

 

}

 

catch (NumberFormatException e) { //Amount parameter was not a number

 

}

 

synchronized(act) {

 

if (req.getParameter(“WITHDRAW”) != null) && (amt < act.balance)) act.balance = act.balance – amt;

 

if (req.getParameter(“DEPOSIT”) != null) && (amt > 0) act.balance = act.balance + amt;

 

} //end synchronized block

 

 

doGet(req, resp);          // Show ATM screen

 

}   // end doPost()

 

 

public void destroy() {

 

// save balance to a file before servlet is unloaded. If servlet used JDBC to // write to a database, need to destroy all database resources here.

 

}

 

 

class Account {

 

public int balance;

 

}   }

 

 

Server-Side Includes

 

Servlets are not confined to handling entire requests. Some web servers allow servlets to add small amounts of dynamic content to otherwise static HTML pages. E.g. a server-side include to add a randomly selected advertisement to a page. A page that uses the advertisement servlet is written like a normal HTML page, except that it contains a <SERVLET> tag and is saved with the .shtml extension. When a client requests a .shtml page, the server finds all the <SERVLET> tags and replaces them with the output from the appropriate servlets.

 

When using a <SERVLET> tag, you must include a CODE parameter that identifies the servlet to be loaded. This can be a class name or a servlet alias set up within the server.

 

Example 7

 

 

Here’s a sample .shtml file that uses a servlet with server-side include.

 

 

<HTML>

 

<HEAD><TITLE>Today’s News</TITLE></HEAD>

 

<BODY>

 

<H1>Headlines</H1>

 

<H2>Java servlets take over the web! </H2> <SERVLET CODE=AdMaker>

 

<PARAM NAME=pagetitle VALUE=”Headlines”>

 

</SERVLET>

 

</BODY>

 

</HTML>

 

 

The AdMaker servlet is as follows:

 

import javax.servlet.*;                                 import javax.servlet.http.*;        import java.io.*;

 

 

public class AdMaker extends HttpServlet {

 

static String[] adText = {“Al’s Web Services”, “Bob’s House of HDs”, “Main St. Computers”};

 

int currentAd = 0;

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 

String adContent;

 

PrintWriter out = resp.getWriter();

 

synchronized(this) { adContent = adText[currentAd]; currentAd++;

 

if (currentAd >= adText.length) currentAd = 0;

 

}

 

String title = req.getParameter(“pagetitle”);        // title of the .shtml page

 

I       if (title != null)

 

out.println(title + “ is brought to you by”);

 

else

 

out.println(“This page is brought to you by”); out.println(adContent);

 

}

 

}

 

 

Server-side includes can be a powerful tool but are not part of the standard Servlet API, and therefore some servlet implementations may not support them. JavaServer Pages (JSP) is another technology for accessing server-side Java components directly in HTML pages. This is similar to Active Server Pages.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Applets : Servlet Basics |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.