JAVA N WEB TECH SYLLABUS

Thursday, January 13, 2011

Seesion in servlets

HTTP is a stateless protocol. Each request is independent of the previous one. However,
in some applications, it is necessary to save state information so that information can
be collected from several interactions between a browser and a server. Sessions provide
such a mechanism.

A session can be created via the getSession( ) method of HttpServletRequest. An
HttpSession object is returned. This object can store a set of bindings that associate
names with objects. The setAttribute( ), getAttribute( ), getAttributeNames( ), and
removeAttribute( ) methods of HttpSession manage these bindings. It is important
to note that session state is shared among all the servlets that are associated with a
particular client.
The following servlet illustrates how to use session state. The getSession( ) method
gets the current session. A new session is created if one does not already exist. The
getAttribute( ) method is called to obtain the object that is bound to the name “date”.
That object is a Date object that encapsulates the date and time when this page was last
accessed. (Of course, there is no such binding when the page is first accessed.) A Date
object encapsulating the current date and time is then created. The setAttribute( )
method is called to bind the name “date” to this object.


removeAttribute( ) methods of HttpSession manage these bindings. It is important
to note that session state is shared among all the servlets that are associated with a
particular client.
The following servlet illustrates how to use session state. The getSession( ) method
gets the current session. A new session is created if one does not already exist. The
getAttribute( ) method is called to obtain the object that is bound to the name “date”.
That object is a Date object that encapsulates the date and time when this page was last
accessed. (Of course, there is no such binding when the page is first accessed.) A Date
object encapsulating the current date and time is then created. The setAttribute( )
method is called to bind the name “date” to this object.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DateServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Get the HttpSession object.
HttpSession hs = request.getSession(true);
// Get writer.
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.print("<B>");
// Display date/time of last access.
Date date = (Date)hs.getAttribute("date");
if(date != null) {
pw.print("Last access: " + date + "<br>");
}
// Display current date/time.
date = new Date();
hs.setAttribute("date", date);
pw.println("Current date: " + date);
}
}

When you first request this servlet, the browser displays one line with the current
date and time information. On subsequent invocations, two lines are displayed. The
first line shows the date and time when the servlet was last accessed. The second line
shows the current date and time.

Wednesday, January 5, 2011

Program to request server information viz. Request Method, URL, Protocol and Remote address usin Servlet

Servlet File
ServerInfo.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServerInfo extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    {
        response.setContentType("text/html");
        PrintWriter pw=response.getWriter();
        pw.println("protocol"+request.getProtocol());
        pw.println("<br>remote address"+request.getRemoteAddr());
        pw.println("<br>URI"+request.getRequestURI());
        pw.println("<br>remote address"+request.getMethod());
        pw.close();
    }
}

Program to create and display a cookie usin servlet

Html File
Cookie.html

 <html>
<head><title>COOKIE INFORMATION</title></head>
<body><form method="Get" action="http://192.168.8.3:8080/servlet/MyCookie">
<input type="text" name="txtcookie">
<input type="submit">
</form>
</body>
</html>

Servlet File
MyCookie.java






import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyCookie extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
String daa=req.getParameter("txtcookie");
Cookie coo=new Cookie("naveen",daa);
res.addCookie(coo);
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<B>mycookie has been set to");
pw.println(daa);
pw.close();
}
}

Program to change the background color of the page on the color selected by the user usin servlet.

Html File
Colour.html

<html><body bgcolor="blue">
<form action="http://192.168.8.3:8080/servlet/Colour">
<select name="color" size="3">
<option value="red">Red</option>
<option value="magenta">Magenta</option>
</select>
<input type="Submit" value="Submit">
</form>
</body>
</html>

Servlet File
Colour.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Colour extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
String col=req.getParameter("color");
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.println("<html><body bgcolor=" +col+">");
out.println("<b>SELECTED COLOR IS</b>");
out.println("<b>"+col+"</b>");
out.println("</body></html>");
out.close();
}
}

Program to accept user name and display a greeting message using servlet

Html file 
Name.html
<html>
<body bgcolor="blue">
<form action="http://192.168.8.3:8080/servlet/Greeting " method="get">
<input type="text" name="uname">
<input type="Submit" value="Submit">
<input type="Reset" value="Reset"></form>
</body>
</html>

Servlet File
Greeting.java



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Greeting extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
String username=req.getParameter("uname");
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.println("Welcome"+username);
out.close();
}
}