| Author |
regarding session transfer from servlet to jsp
|
dineshiop kumar
Greenhorn
Joined: Mar 05, 2009
Posts: 13
|
|
i have two jsp and one servlets once the username is been entered i want the session to be creaed in servlet and be transfered to second jsp
here is the first jsp
<html>
12 <head>
13 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
14 <title>JSP Page</title>
15 </head>
16 <body>
17 <form method="POST" action="../WebApplication3/session">
18 <table border="1">
19
20 <tbody>
then a servlet
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author sys4
*/
public class session extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
HttpSession session = request.getSession(true);
session.setAttribute("username", out);
RequestDispatcher rd = request.getRequestDispatcher("/second.jsp");
rd.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
third jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
session.getAttribute("username");
%>
hello
</body>
</html>
|
 |
Nishan Patel
Ranch Hand
Joined: Sep 07, 2008
Posts: 676
|
|
Hi,
Just change your session.setAttribute("username", out);
to
session.setAttribute("username", username);
This will set your session with user name.
|
Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
|
 |
 |
|
|
subject: regarding session transfer from servlet to jsp
|
|
|