从servlet中访问存储在jsp:useBean中的表单数据

时间:2013-05-24 06:19:40

标签: jsp servlets javabeans

当用户在UserBean.java上提交登录凭据时,我正在尝试填充包lp}中的bean类(Login.jsp)。 然后从servlet(ConfimUserDetails.java中的lp)访问此bean类 我无法从Servlet中访问bean类的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()中的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();
        }
    }

1 个答案:

答案 0 :(得分: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可以派上用场来减少样板代码。

另见: