当用户提交登录凭据时,我试图填充一个 bean 类(UserBean.java
在包中) 。然后从 servlet(也在包中)访问这个 bean 类。
我无法从 Servlet 中访问 bean 类的和方法。lp
Login.jsp
ConfimUserDetails.java
lp
getUid()
getPassword()
这是我的所有代码:
Login.jsp
为了将表单数据映射到 bean 类,我在其中放置了以下内容。
<%@ page import="lp.UserBean" %>
<jsp:useBean id="ubean" class="lp.UserBean" scope="session"/>
<jsp:setProperty name="ubean" property="*"/>
<form method="post" action="/TestServlet/ConfirmUserDetails">
<table align="center">
<tr>
<th >Enter </th>
<th>Login Details</th>
</tr>
<tr>
<td >UserID : </td>
<td ><input type="text" name="uid" size="15" maxlength="8" /></td>
</tr>
<tr>
<td >Password : </td>
<td ><input type="Password" name="password" size="15" maxlength="20" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
doPost()
in (ConfirmUserDetails.java
接收 servlet)如下所示:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession();
UserBean userBean;
userBean = (UserBean)session.getAttribute("ubean");
/*
if (session.getAttribute("ubean") != null) {
userBean = (UserBean)session.getAttribute("ubean");
} else {
System.out.println("bean did not get picked up-so lets create one");
userBean = new UserBean();
session.setAttribute("ubean", userBean);
}
*/
UID = userBean.getUid();
Password=userBean.getPassword();
PrintWriter out = response.getWriter();
out.println(UID);
try{
cub = new ConfirmUserBean();
cub= dcm.getUserData(UID,Password);
}
catch (Exception npe){
System.out.println("something went wrong here");
npe.printStackTrace();
}
我已经在servlet中导入了上面的bean类(import lp.UserBean;)
Login.jsp
页面加载正常。然后当我点击提交时,我得到一个NullPointerException
.
这是堆栈跟踪 - 我使用的是 Eclipse 和 Tomcat:
May 24, 2013 7:03:00 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [lp.ConfirmUserDetails] in context with path [/TestServlet] threw exception
java.lang.NullPointerException
at lp.ConfirmUserDetails.doPost(ConfirmUserDetails.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
如果您想查看我的 servlet 的顶部:
public class ConfirmUserDetails extends HttpServlet {
private ConfirmUserBean cub;
private DataConnectionManager dcm;
private String UID;
private String Password;
private UserBean userBean;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
dcm = new DataConnectionManager();
dcm.setDBURL("jdbc:mysql://localhost:3306/loans");
dcm.setDBUserName("lalinP");
dcm.setDBPassword("lalin");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("driver could not be loaded");
e.printStackTrace();
}
}