0

当用户提交登录凭据时,我试图填充一个 bean 类(UserBean.java在包中) 。然后从 servlet(也在包中)访问这个 bean 类。 我无法从 Servlet 中访问 bean 类的和方法。lpLogin.jspConfimUserDetails.javalp
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();
        }
    }
4

1 回答 1

0

您似乎期望在调用 servlet之前<jsp:setProperty>将所有请求参数设置为 bean 属性。这不是真的。您必须自己收集和设置请求参数(或者选择一个自动完成这一切的 MVC 框架,例如 JSF、Spring MVC 等)。

仅当<jsp:setProperty>JSP 中的特定行由 JSP 引擎执行时才执行。当您将表单提交给 servlet 时,这只会在 servlet 转发到特定 JSP 时发生,这为时已晚。

摆脱<jsp:useBean><jsp:getProperty>完全。当您已经在使用前端控制器 servlet 时,它们是无用的并且只会令人困惑。您需要自己在 servlet 中创建和管理 bean。使用 MVC 框架或 Apache Commons BeanUtils 可能会派上用场,以减少样板代码。

也可以看看:

于 2013-05-24T11:05:54.057 回答