无法使用servlet和jsp中的特定用户名和密码登录

时间:2016-05-04 14:45:58

标签: mysql jsp servlets

下面是我的Servlet和jsp代码。我正在尝试使用用户名“admin”和密码“pass”登录,但我收到了NullPointerException。以下是使用浏览器登录时收到的错误。

Servlet代码:

package org.fproject;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AdminLogin
 */
@WebServlet(value="/AdminLogin",initParams={@WebInitParam (name="user", value="admin"),
        @WebInitParam(name="pass",value="pass")})
public class AdminLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String iuser=getInitParameter("user");
        String ipass=getInitParameter("pass");
        String user=req.getParameter("user");
        String pass=req.getParameter("pass");
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        out.println("<html><body>");
        if(user.equals("admin") && pass.equals("pass")){
            out.println("<b>Welcome<b>");
        }
        else{
            out.println("<b>Invalid Login<b>");

        }
        out.println("</body></html>");
        out.close();
    }

    }

JSP代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="org.fproject.*"%>
<!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>Insert title here</title>
</head>
<body>
<form action="AdminLogin"  method="post">
     <table border="0">
     <tr>
<td>Username<td>
<td><input type="text" name="user" size="50"/></td>
</tr>
<tr>
<td>Password<td>
<td><input type="password" name="password" size="50"/></td>
</tr>
<tr>
<td><input type= "submit" value="SUBMIT"/></td>
</tr>
</table>
</form>
</body>
</html>

错误: -

  

HTTP状态500 -

     

输入例外报告

     

消息

     

description服务器遇到阻止的内部错误()   它来自履行这一要求。

     

例外

     

显示java.lang.NullPointerException     org.fproject.AdminLogin.doPost(AdminLogin.java:48)     javax.servlet.http.HttpServlet.service(HttpServlet.java:754)     javax.servlet.http.HttpServlet.service(HttpServlet.java:847)注意   JBoss中提供了根本原因的完整堆栈跟踪   Web / 7.0.13.Final logs。

     

JBoss Web / 7.0.13.Final

1 个答案:

答案 0 :(得分:0)

替换

<input type="password" name="password" size="50"/>

通过

<input type="password" name="pass" size="50"/>

来自jsp页面

或者

更改

String pass=req.getParameter("pass");

通过

String pass=req.getParameter("password");
来自Servlet的