preethi A
05-03-2008, 09:43 AM
Hi ,
I am new to servlets.I am having a jsp page to login with username and password.I have to get the username and password in a servlet page using session.How to start with this?Any url or sample code .please suggest me.
Thanks.
amitthechosen1
05-06-2008, 02:43 PM
There is some good reading available on the net for the problem stated below:-
http://forum.java.sun.com/thread.jspa?threadID=694137&messageID=4033625
http://java.sys-con.com/read/37125.htm
http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html
The code posted below is from Tomcat's , webapps examples, that is available on downloading it.
session.setAttribute and session.getAttribute are the two functions used as setter and getter for information from one jsp page to the other, in a session.
Source Code for Session Example
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
// print session info
Date created = new Date(session.getCreationTime());
Date accessed = new Date(session.getLastAccessedTime());
out.println("ID " + session.getId());
out.println("Created: " + created);
out.println("Last Accessed: " + accessed);
// set session info if needed
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
// print session contents
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println(name + " = " + value);
}
}
}
sballepu
07-25-2008, 09:13 PM
Hi,
If you need a good servlets video tutorial, you can go here
http://www.sharmanj.com
Thanks,
Sharad.
If I Were A Boy
12-10-2008, 12:57 PM
The servlet you are accessing thruogh JSP should be configured in web.xml file
servlet
01-08-2009, 06:34 AM
You can do this using request.getParameter().
have a look at Servlet examples (http://www.jsptube.com) it has examples which will help you.