1

I know similar questions have been asked earlier but for some reason it is not working. I am just trying to check if user has entered both the fields on the login page or not. If not, then I want to display the message on jsp saying that they need to enter both the credentials. Here is my JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
    <h2>Some App</h2>
    <form action="login" method="post">
        <table>
            <tr>
                <td>Username</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" id="pass" name="pass"></td>
            </tr>
        </table>
        <br> <input type="button" value="Submit">
    </form>
    <c:out value= "${error}"/>
</body>
</html>

Then here is the servlet:

@WebServlet("/login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        //Getting values from the form
        String username = request.getParameter("uname");
        String password = request.getParameter("pass");

        System.out.println("Username is: "+ username);
        System.out.println("Password is: "+ password);

        //User user = new User();

        if((username.equals(""))|| password.equals("")){

            String message = "Please enter both the credentials";
            request.setAttribute("error", message);
            //RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
            //rd.forward(request, response);
            getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);

        }
        else{
            RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
            rd.forward(request, response);
        }

        //Setting values in the session
        session.setAttribute("username", username);
        session.setAttribute("password", password);
    }
}

Since I am trying to experiment with maven, here is my pom.xml file with dependency added for jstl jar:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Why does that c:out tag not print anything? What am I doing wrong? Any help will be appreciated. Thank You.

user3044240
  • 621
  • 19
  • 33
  • What do you see as your output? One way you can debug is to put some System.out.println() values in your method to make sure you're reaching the desired areas. Also, note that you capitalized the "I" in your "index.jsp" – riddle_me_this May 15 '15 at 01:48
  • @bphilipnyc I am unable to reach servlet. Please check my response to NaMaN user below. I am also updating my code now for servlet. Thanks. – user3044240 May 15 '15 at 17:54

4 Answers4

0

You are mixing two styles of returning a response to the client. Try passing the error message using this code:

if((username == null)|| password == null) {              
    String message = "Please enter both the credentials";
    request.setAttribute("error", message);
    getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}

The rule of thumb is that you have to use request.setAttribute() with forward() and request.getSession().setAttribute() with response.sendRedirect().

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

try ${sessionScope['error']} instead of ${error}. If that worked then you may have another attribute in another scope with the same name 'error'

0

You should also check if username or password are empty. i.e. (username != null && username.trim().isEmpty())

ktorn
  • 1,032
  • 8
  • 14
0

Suggestions from everyone was very valuable. There were a few things that I was doing wrong: incorrect servlet mapping (@WebServlet fixed it), changed code to what @NaMaN and @bphilipnyc replied and the biggest mistake due to which I wasn't able to reach servlet was that I had input type as button in jsp. I changed it to input type submit, and then it worked. Thank you guys for replying. Really appreciate it.

user3044240
  • 621
  • 19
  • 33
  • You can do button type="submit" if you need that: http://stackoverflow.com/questions/7117639/input-type-submit-vs-button-tag-are-they-interchangeable – riddle_me_this May 15 '15 at 22:05