Click here to Skip to main content
13,508,219 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have an assignment of creating a login page.if the username and password is dolly it should redirect me to another servlet page telling valid user else it should show invalid user.
This is what i have done.The problem is i do not know how to redirect it to the other page.
It is redirecting to the other page but i want whether a valid or invalid user to be
displayed over there.
Please help me out.

package servlets;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})

public class NewServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {

            out.println("<title>Servlet NewServlet</title>");
            out.println("<form action=http://localhost:9999/WebApplication4/NewServlet1 method=GET>");
            out.println("Login Form");
            out.println("Enter Username:<input type=text name=t1>");
            out.println("Enter password:<input type=password name=t2>");
            out.println("<input type=submit  value=submit");
        } finally {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        PrintWriter pw=response.getWriter();
          String user=request.getParameter("t1")  ;
              String pass=request.getParameter("t2");
          if(user.equals("dolly") && pass.equals("dolly"))
             pw.println("Valid user");
            pw.println("Invalid user");

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    public String getServletInfo() {
        return "Short description";

[torsten]added code block[/torsten]
Posted 28-Dec-12 8:04am
Updated 28-Dec-12 8:23am
Member 8512273 28-Dec-12 14:13pm
I have used annotations in the above code.So there is no need of web.xml

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Shouldn't the server validate the name & password? I guess so.

So let the server answer with a suitable return value:
In case of true / "pass" you should grant access, in case of false / "no pass" you should present another page.


So you do NOT do Server-Requests. Then you should also not use a HTTPServlet.

public class SimpleServlet extends GenericServlet {

 public void service(ServletRequest request, ServletResponse response)
        throws ServletException, IOException {

    // figure values from client's request
    String strParam = request.getParameter("param");
    String strNick  = request.getParamter("nick");
    String strPW  = request.getParamter("pw");

    if("login".equals(strParam) ){ // ask first, cause strNick & strPW could be NULL
        // present login
    else if("dolly".equals(strNick) && "dolly".equals(strPW) ){ // login values fine
        response.getWriter().write("<html><body>Login granted</body></html>");
    else { // login not granted

You simply present login (as you do in your code now) and call an internal function on click "login" to check the static nick/pw.
After that, you present
Member 8512273 28-Dec-12 14:45pm
But over here we are taking static values of username and password.that is if the username is dolly and if the password is dolly it is valid else it is not.Whether it is valid or invalid should be printed in a seperate servlet.
TorstenH. 28-Dec-12 14:53pm
Then I don't even know why you are using a HTTPServlet with the methods doGet and doPost in there.
classic mislead selection of technique?

I will update the answer and show you a way to do it proper.
Here we go, please check out the "Edit"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180417.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100