is this(code inside) a good approach for a system login?

package user;

import java.io.IOException;

import java.sql.*;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import user.UserBean;

publicclass LoginServletextends HttpServletimplements Servlet

{

/* (non-Javadoc)

* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

*/

protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

String username = request.getParameter("username");

String password = request.getParameter("password");

String url ="DATABASE URL";

UserBean user =null;

try

{

// check if user exist

}

catch(Exception e)

{

e.printStackTrace();

}

if (user !=null)

{

response.sendRedirect("../controlpanel.jsp");

}

// otherwise the user was not in the database

else

{

String message ="The name and user you attempted to login with do not exist in the database or you mistyped your password. Please try again.";

request.getSession().setAttribute("login_failed", message);

String URIpath = request.getRequestURI();

request.setAttribute("path", URIpath);

getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);

}

}

protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

doPost(request,response);

}

}

the problem is if i make redirection to control panel as a getServletContext().getRequestDispatcher().forward(request, response), users may be confused because the url will be the same as with the error page for login.. do you have any suggestions?

[3518 byte] By [hardcodera] at [2007-11-27 2:36:57]
# 1
Use HttpServletResponse#sendRedirect() instead.By the way, there is no need to add "implements Servlet" as the HttpServlet already does it.
BalusCa at 2007-7-12 2:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
do i use it on both cases? thanks for the reply!!
hardcodera at 2007-7-12 2:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
i tried what you said and worked great! one question though.. how do i remove the values from the session? for example, when the user decides to go to another website and comes back, the error message will not be posted or it will be reset...
hardcodera at 2007-7-12 2:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> do i use it on both cases?

One approach I like is

public void doPost(HttpServletRequest request, HttpServletResponse response){

processRequest(request,response);

}

public void doGet(HttpServletRequest request, HttpServletResponse response){

processRequest(request,response);

}

private void processRequest(HttpServletRequest request, HttpServletResponse response){

//write code here

}

The above keeps both doPost and doGet clean and clutter free.

appy77a at 2007-7-12 2:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> how do i remove the values from the session?

session.removeAttribute does the trick also instead of setting it to the session consider setting it as Http Request attribute.

> for example, when the user decides to go to another

> website and comes back, the error message will not be

> posted or it will be reset...

If you follow the MVC / Front Controller http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html pattern then, you can remove the value from the session inside the Action class that process the request to this JSP page. The code to remove it from the session would be towards the top of the action class.

appy77a at 2007-7-12 2:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
sir, what do you mean by setting it as Http request? how do i do this? thanks for your replies
hardcodera at 2007-7-12 2:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...