Friday, July 15

using include and forward method in servlet

index.jsp file
__________________

~%@page contentType="text/html" pageEncoding="UTF-8"%#
~!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"#

~html#
~head#
~meta http-equiv="Content-Type" content="text/html; charset=UTF-8"#
~title#JSP Page~/title#
~/head#
~body#
~h1#Hello World!~/h1#
~form action="servlet" method="post"#
Name: ~input type="text" name="name"value="" /#
Password: ~input type="password" name="password" value=""/#
~input type="submit" value="Login"/#
~/form#
~/body#
~/html#
___________________________________________________________________

servlet.java
______________________________
package server;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
String password=request.getParameter("password");

if(name.equals("prembharti")&& password.equals("school"))
{
out.println("~html#");
out.println("~body#");
out.println("~h1# Servlet Page");
out.flush();
ServletContext context= getServletContext();
RequestDispatcher rd = context.getRequestDispatcher("/include");
rd.include(request, response);
out.println("~html#");
out.println("~body#");
out.println("I m again servlet page...!!");
}
else
{
//response.sendRedirect("index.jsp");
ServletContext scontext=getServletContext();
RequestDispatcher rd2=scontext.getRequestDispatcher("/forward");
String ename=name.replace('e','i');
request.setAttribute("ename", ename);
rd2.forward(request, response);
}
}
}
__________________________________________________________________
include.java
_______________________________________
package server;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class include extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("~marquee# I am included ~/marquee#");
}
}
__________________________________________________________________________
forward.java
__________________________________________________________________________
package server;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class forward extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String ename=(String)request.getAttribute("ename");
String password = request.getParameter("password");

try {

out.println("~html#");
out.println("~head#");
out.println("~title#Servlet forward~/title#");
out.println("~/head#");
out.println("~body#");
out.println("~h1#Servlet forward at " + request.getContextPath () + "~/h1#");
out.println("My name is :"+ ename + "password is :" + password);
out.println("~/body#");
out.println("~/html#");
} finally {
out.close();
}
}
}

No comments: