aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

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 java.io.IOException;
import java.io.PrintWriter;

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")) {

session.setAttribute("login","success");
// out.println( " <b> Hello " + username + "</b>");
response.sendRedirect("success.jsp");
}

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

RequestDispatcher dispatch = request
.getRequestDispatcher("failure.jsp");
dispatch.forward(request, response);
session.setAttribute("login","faliure");
}

// 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 success....it shows the end URL..so 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
Sheriff

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
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

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] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can u help me with this problem ..and how to implement