Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic