File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes can u help me with this problem ..and how to implement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "can u help me with this problem ..and how to implement" Watch "can u help me with this problem ..and how to implement" New topic

can u help me with this problem ..and how to implement

Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Hai ,I am new to Servlets.

I created a form with 2 text boxes,user name and password , submit button.

3 files (index.html , sussess.jsp displaying message as "Ligin Success" and failure.jsp displaying message as ("please chek your user name and psw).

If i give correct user name and psw..its going to success.jsp..and if its failure its going to failure.jsp.


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FirstServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

HttpSession session = request.getSession(true);

String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
if (username.equals("xyz") && password.equals("abc")) {

// out.println( " <b> Hello " + username + "</b>");

else {
// out.println("<b> Invalid username /password </b>");
// response.sendRedirect("failure.jsp");

RequestDispatcher dispatch = request
dispatch.forward(request, response);

// TODO Auto-generated method stub


I am using redirect if i am success, and forwarding if failure.

whats the problem was is..since i was using redirect for shows the end when I type the URL without going and filling out the form( user name and psw) ..its going to success.jsp page. How can i restrict that..and how can i implement.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Why not forward for both?

I'm assuming you're writing a login procedure for your app.
One common way to implement this is to place an object in session scope after a successful login.
Then, from every page that requires users to be logged in in order to access, check for the existence of that object. If it's null, redirect them to the login page. A ServletFilter makes it easy to apply this logic to all of your pages.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Two things that will help you on the Ranch:

1) Use the UBB code tags to preserve the formatting of your code. Didn;t you ever wonder what that CODE button was below the text entry area?

2) Use real words!

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: can u help me with this problem ..and how to implement
It's not a secret anymore!