H5W3
当前位置:H5W3 > 其他技术问题 > 正文

javabean 为何或取不到值?

最近想看下java web 的开发,刚到第一步就卡着了

login.jsp 页面

<form method="post" action="dologin.jsp">
    usn:<input type="text" name="username" />
    pwd:<input type="text" name="password" />
    <input type="submit" >
</form>

dologin.jsp 页面

<%
    request.setCharacterEncoding("utf-8");

   out.println( request.getParameter("username")+":"+request.getParameter("password"));
%>
<jsp:useBean id="myuser" class="com.test.po.Users" scope="page"></jsp:useBean>
<jsp:useBean id="userdao" class="com.test.dao.UsersDAO" scope="page"></jsp:useBean>

<jsp:setProperty name="myuser" property="username" param="username"></jsp:setProperty>
<jsp:setProperty name="myuser" property="password" param="password"></jsp:setProperty>



<%
    out.println(myuser.getUsername());
%>

java bean type

package com.test.po;
import com.sun.org.apache.xpath.internal.operations.String;
public class Users {
    private String username;
    private String password;
    public Users(){
    }

    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;
    }
}

login.jsp 提交了数据之后出现下面的错误:

HTTP Status 500 – Internal Server Error

Type Exception Report

Message An exception occurred processing [/dologin.jsp] at line [17]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing [/dologin.jsp] at line [17]

14: <jsp:useBean id="myuser" class="com.test.po.Users" scope="page"></jsp:useBean>
15: <jsp:useBean id="userdao" class="com.test.dao.UsersDAO" scope="page"></jsp:useBean>
16: 
17: <jsp:setProperty name="myuser" property="username" param="username"></jsp:setProperty>
18: <jsp:setProperty name="myuser" property="password" param="password"></jsp:setProperty>
19: 
20: 


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:467)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

org.apache.jasper.JasperException: java.lang.IllegalArgumentException: argument type mismatch
    org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:314)
    org.apache.jsp.dologin_jsp._jspService(dologin_jsp.java:136)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.IllegalArgumentException: argument type mismatch
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:308)
    org.apache.jsp.dologin_jsp._jspService(dologin_jsp.java:136)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.0.M26

如果注释掉dologin.jsp 的 java bean 代码,是可以成功获取到 username 和 password

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" %>
<%
    request.setCharacterEncoding("utf-8");

   out.println( request.getParameter("username")+":"+request.getParameter("password"));
%>
<%--
<jsp:useBean id="myuser" class="com.test.po.Users" scope="page"></jsp:useBean>
<jsp:useBean id="userdao" class="com.test.dao.UsersDAO" scope="page"></jsp:useBean>

<jsp:setProperty name="myuser" property="username" param="username"></jsp:setProperty>
<jsp:setProperty name="myuser" property="password" param="password"></jsp:setProperty>



<%
    out.println(myuser.getUsername());
%>
--%>

clipboard.png

回答:

用 eclipse 重写了一遍,没有问题,可能是idea 设置的问题

本文地址:H5W3 » javabean 为何或取不到值?

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址