简体   繁体   中英

Validating a form using JSP

I'm completely new to JSP and am creating a simple form and validating it through JSP. Here's my code :-

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<%@page import="java.io.* , java.sql.* , java.util.* , javax.servlet.*" %>

<body>
    <h1>Login Page by JSP</h1>
    <hr>
    <form method="post" action="index_jsp">
        Enter name : <input type="text" name="user">
        Enter password : <input type="password" name="pass">
        <input type="submit" value="login">
    </form>
    <%
    String user = request.getParameter("user");
    String pass = request.getParameter("pass");

    if(user.equals("admin") && pass.equals("admin"))
    {
    response.sendRedirect("wel.html");
    }

    %>
    <br>
    <a href="new.jsp">Click here !</a>

</body>
</html>

But each and every time I run it , I get a NullPointerException . What am I doing wrong ? Immediate help would be appreciated !

Thanks!

First bit of advice, since you're new to JSP: Don't write scriptlet code. That's all the stuff between <% and %> . It's a 1998 vintage technology that should be discouraged. Learn JSTL and Model-2 MVC instead.

You need a servlet to orchestrate those JSPs. Every web MVC framework that I know of has what's called a front controller servlet to manage the communication and orchestration. You should, too.

You have the form POST in this JSP, sending the HTTP request to index_jsp (bad name). It's executing in the browser on the client machine.

I would expect you to send this to a servlet on the server side that gets the HTTP request, gets the username and password out, compares the values to a database to see if the user is indeed registered. Then I'd either send the next view or route to the "sorry" page. That's not what you're doing.

Don't do equals checks on the variables, rather do them against the constants:

 if("admin".equals(user) && "admin".equals(pass)) {
    response.sendRedirect("wel.html");
 }

This way you cannot get the NPE.

Change this line

if(user.equals("admin") && pass.equals("admin"))
    {
    response.sendRedirect("wel.html");
    }

with

if(user!=null){

    if(user.equals("admin") && pass.equals("admin"))
    {
    response.sendRedirect("wel.html");
    }
}

You are not checking null for request attributes.

replace

String user = request.getParameter("user");
String pass = request.getParameter("pass");

with

String user = request.getParameter("user") != null ? request.getParameter("user") : "";
String pass = request.getParameter("pass") != null ? request.getParameter("pass") : "";

change code:

if("admin".equals(user) && "admin".equals(pass))  {
    response.sendRedirect("wel.html");
}

When you first run jsp,it will execute the code,but user and pass is null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM