我用struts写的一个简单的用户登录验证,为什么总是报的空指针异常呢?
其中action中的代码如下我没有联系数据库,只是简单的验证操作没有涉及dao里边的东西packageaction;importcom.opensymphony.xwor...
其中action中的代码如下
我没有联系数据库,只是简单的验证操作没有涉及dao里边的东西
package action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloReader extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String logincheck() throws Exception {
if (username.equals("wang") && password.equals("123")) {
return SUCCESS;
}
return INPUT;
}
}
struts中的配置文件如下struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="action" extends="struts-default">
<action name="login" class="action.HelloReader" method="logincheck" >
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
还有两个jsp页面,分别是
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" method="post" theme="simple">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="登陆"></s:submit>
</s:form>
</body>
</html>
和success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登陆成功!!</h1>
</body>
</html>
异常如下
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at action.HelloReader.logincheck(HelloReader.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
。。。。。。。 展开
我没有联系数据库,只是简单的验证操作没有涉及dao里边的东西
package action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloReader extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String logincheck() throws Exception {
if (username.equals("wang") && password.equals("123")) {
return SUCCESS;
}
return INPUT;
}
}
struts中的配置文件如下struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="action" extends="struts-default">
<action name="login" class="action.HelloReader" method="logincheck" >
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
还有两个jsp页面,分别是
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" method="post" theme="simple">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="登陆"></s:submit>
</s:form>
</body>
</html>
和success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登陆成功!!</h1>
</body>
</html>
异常如下
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at action.HelloReader.logincheck(HelloReader.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
。。。。。。。 展开
4个回答
展开全部
你在checklogin里面判断前 先把username 和password输出来看看
估计你action里面这两个都没得到值 所以才空指针
估计你action里面这两个都没得到值 所以才空指针
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
username.equals("wang") && password.equals("123")
改成
"wang".equals(username) && "123".equals(password)
再试试
改成
"wang".equals(username) && "123".equals(password)
再试试
追问
嗯 那个问题可以了,
请问我刚才那样写不对吗,以前都是这样写的。。都没有错
还有就是login.jsp中的
要写成
用户名:
密码:
才会正常显示出用户名和密码
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置一个breakpoint 调试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询