First JSP

I have a JSP - which accepts username and password for a website that does some online trading.

On clicking the submit button, I call a servlet that processes this request.

public class Login extends HttpServlet

{

public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException

{

String username, password;

boolean response = false;

// Gets username and password from the Login page.

userrname = req.getParameter("username");

password = req.getParameter("password");

//Using "ct" which was stored in ServletContext, validate the username and password.

CheckTable ct = (CheckTable)(getServletContext().getAttribute("ct"));

response = ct.validate(username,password);

//If user is found in the table, save his session and go to WelcomePage.jsp.

if (response == true)

{

session.setAttribute("username", username);

RequestDispatcher dp = getServletContext().getRequestDispatcher("/WelcomePage.jsp");

dp.forward(req,res);

}

// else return to LoginPage.jsp giving message- username not found in system or passowrd incorrect.

else

{

}

}

}

What are the changes I need to make for the above? How would I code the else part?

[1330 byte] By [bobbykale] at [2007-9-30 4:13:13]
# 1

Hey boobykale,

In the else, I would do something like this:

// note that %20 is a URL encoded space character.

res.sendRedirect("LoginPage.jsp?errorMessage=username%20not%20found%20in%20system%20or%20passowrd%20incorrect")

You will have to edit your LoginPage.jsp to check for the errorMessage attribute and display it if it is there.

Hope this helps.

James.

maaamgbtdbsbe

jrlaughlin at 2007-7-1 12:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thank you.In The LoginPage.jsp how would I check for the error message?String errorMessage;if (errorMessage().length ==0))//use HTML tags and display message
bobbykale at 2007-7-1 12:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

try something like this:

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

// then inside your HTML...assuming you are using a table for layout

<tr>

<td style="color:red"><%= errorMessage %></td>

</tr>

Hope this helps.

James.

jrlaughlin at 2007-7-1 12:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...