How to sent variable value from servlet to jsp?
[nobr]Hi, everyone!
I am new in servlets and jsp. Please help me.
I have following simplified servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
publicclass Registrationextends HttpServletimplements SingleThreadModel{
publicvoid service(HttpServletRequest request, HttpServletResponse response){
String first_name = (String)request.getParameter("first_name");
String contextPath = request.getContextPath();
if (first_name!=null)
try{
if (first_name.equals("ameli"))
{
getServletConfig().getServletContext().getRequestDispatcher("/correctlogin.jsp").forward(request, response);}
else
{
getServletConfig().getServletContext().getRequestDispatcher("/errorlogin.jsp").forward(request, response);
}
}
catch(Exception exc){
exc.printStackTrace();
}
}
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
service(request, response);
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
service(request, response);
}
}// end of class
Here "first_name" is taken from registration.jsp.
If correctlogin.jsp is called through servlet, 'first_name' variable should be sent to this page.
Code for correctlogin.jsp is as follows:
<%@ page contentType="text/html; charset=windows-1251" import="javax.servlet.*"%>
<html>
<head>
<title>TITLE</title>
</head>
<%
try{
String nick = (String)request.getAttribute("first_name");
%>
<br>
User name:
<b><%= nick %></b>
<%
}
catch(Exception exc){exc.printStackTrace();}
%>
</body>
</html>
As the result, (String)request.getAttribute("first_name"); does not work.
Please help me.
What are the main steps of declaring such variables that might be sent from servlet to jsp?
Thanks a lot in advance[/nobr]

