日期:2014-05-19  浏览次数:20674 次

一个servlet得到jsp控件值报空指针
servlet页面:
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");

String name=request.getParameter("user");
String password=request.getParameter("password");
if(name.equals("")||password.equals("")){
response.sendRedirect("index.jsp");
}
else{
if(name.equals("jheng") && password.equals("123456")){
response.sendRedirect("success.jsp");
HttpSession session =request.getSession();
session.setAttribute("user", name);
}
else{
response.sendRedirect("error.jsp");
}
}
}


}
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>登陆页面</title>
  </head> 
  <body>
  <form action="login" method="post">
  <table>
  <tr>
  <td>用户名</td>
  </tr>
  <tr>
  <td><input type="text" name="user" id="user"></td>
  </tr>
  <tr>
  <td>密码</td>
  </tr>
  <tr>
  <td><input type="password" name="password" id="password"></td>
  </tr>
  <tr>
  <td><input type="submit" value="提交"></td>
  </tr>
  </table>
  </form>
  </body>
</html>



------解决方案--------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.niit.servlet.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>这个是servlet访问地址,jsp页面上form表单action="login"提交表单到这个servlet中 你原来写的"/*"struts1里才用吧
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

------解决方案--------------------
探讨
大家貌似没懂我的意思,我把<servlet-name>LoginServlet</servlet-nam