import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginHandler extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); // Get the user's name and password String name = req.getParameter("name"); String passwd = req.getParameter("passwd"); // Check the name and password for validity if (!allowUser(name, passwd)) { out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>"); out.println("<BODY>Your login and password are invalid.<BR>"); out.println("You may want to <A HREF=\"/login.html\">try again</A>"); out.println("</BODY></HTML>"); } else { // Valid login. Make a note in the session object. HttpSession session = req.getSession(true); session.putValue("logon.isDone", name); // just a marker object // Try redirecting the client to the page //he first tried to access <font color=red> try { String target = (String) session.getValue("login.target"); if (target != null) res.sendRedirect(target); return; } catch (Exception ignored) { }</font> // Couldn't redirect to the target. //Redirect to the site's home page. res.sendRedirect(req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()); } } protected boolean allowUser(String user, String passwd) { return true; // trust everyone } } Hi, I have a question about the above code. A person that tries to log in for the first time will have "taget" to be null. So, for a first timer, "target" will always be null, and there is no way to get around it to get a valid link? Can someone please explain this to me? Thanks!
ray bond
Ranch Hand
Joined: Oct 11, 2000
Posts: 111
posted
0
you have to pass target value in parameter to servlet , based on that parameter get string and then send redirect.
Bob Moranski
Ranch Hand
Joined: Nov 22, 2000
Posts: 177
posted
0
This servlet is called by a HTML page that has only two parameters, name and passwd. So...