SOURCE CODE:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Greeting1 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html; charset=\"UTF-8\"");
PrintWriter servletOut = res.getWriter();
HttpSession session=req.getSession();
String signIn=(String)session.getAttribute("signIn");
if(session.isNew() || (signIn==null))
{
printSignInForm(servletOut,"Greeting1");
}
else
{
printWelcomeBack(servletOut,signIn);
}
servletOut.close();
}
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text.html; charset=\"UTF-8\"");
PrintWriter servletOut=res.getWriter();
String signIn=req.getParameter("signIn");
HttpSession session=req.getSession();
if(signIn!=null)
{
printThanks(servletOut,signIn,"Greeting1");
session.setAttribute("signIn",signIn);
}
else
{
printSignInForm(servletOut,"Greeting1");
}
servletOut.close();
}
private void printWelcomeBack(PrintWriter servletOut,String action)
{
servletOut.println(
"<!DOCTYPE html \n" +
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" +
" \"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd\"> \n" +
"<html xmlns='http://www.w3.org/1999/xhtml'> \n" +
" <head> \n" +
" <title> \n" +
" Greeting1.java \n" +
" </title> \n" +
" </head> \n" +
" <body> \n" +
" <p> \n" +
" Welcome back!You are \n" +action+
" arent you?\n"+
" </p> \n" +
" </body> \n" +
"</html> ");
servletOut.close();
}
private void printThanks(PrintWriter servletOut,String action,String action2)
{
servletOut.println(
"<!DOCTYPE html \n" +
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" +
" \"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd\"> \n" +
"<html xmlns='http://www.w3.org/1999/xhtml'> \n" +
" <head> \n" +
" <title> \n" +
" ServletHello.java \n" +
" </title> \n" +
" </head> \n" +
" <body> \n" +
" <p> \n" +
" Thanks for signing in! \n" + action+
" </p> \n" +
"<p>\n"+
"Please\n"+
"<a href='"+action2+" '/>\n"+
" visit again!\n"+
"</p>\n"+
" </body> \n" +
"</html> ");
servletOut.close();
}
private void printSignInForm(PrintWriter servletOut,String action)
{
servletOut.println(
"<!DOCTYPE html \n" +
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" +
" \"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd\"> \n" +
"<html xmlns='http://www.w3.org/1999/xhtml'> \n" +
" <head> \n" +
" <title> \n" +
" Greeting1.java \n" +
" </title> \n" +
" </head> \n" +
" <body> \n" +
"<form method='post' action='"+action+"'><div>\n"+
"<label>\n"+
"Please sign in:<input type='text' name='signIn'/>\n"+
"</label>\n"+
"<br>\n"+
"<input type='submit' name='action' value='SignIn'/>\n"+
"</form>\n"+
" </body> \n" +
"</html> ");
servletOut.close();
}
}
No comments:
Post a Comment