我在JSP中引用JavaBean时遇到错误.. useBean类属性Controller.SampleBean的值无效

时间:2016-02-11 12:43:55

标签: java jsp servlets

不知何故,JavaBean未在home.jsp中引用以下错误

  

SEVERE:Servlet jsp的Servlet.service()抛出异常   org.apache.jasper.JasperException:/home.jsp(第13行,第0列)   useBean类属性Controller.SampleBean的值是   无效。

针对home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.*" %>
<%--     <%@taglib uri="http://java.sun.com/jstl/core" prefix="c"%> --%>
<!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=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<jsp:useBean id="emps" class="Controller.SampleBean" scope="request">
<jsp:setProperty name="emps" property="id" value ="1"/>
<jsp:setProperty name="emps" property="name" value ="Sagar"/>
<p>Emp id is <jsp:getProperty name="emps" property="id" /></p>
<p>Emp name is <jsp:getProperty name="emps" property="name" /></p>
</jsp:useBean>

<% Date d = new Date();%>
<%=d %>

<% %>
</body>
</html>
  

以下是JavaBean类

package Controller;

public class SampleBean {

    private int id;
    private String name;

    public SampleBean(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
  

以下是servlet类

    package Controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.getRequestDispatcher("/home.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

}

1 个答案:

答案 0 :(得分:0)

  1. 班级文件SampleBean应位于WEBINF/classes/Controller下。

  2. 并且应该有public的默认(零)参数构造函数。

  3. 参考:use bean class attribute is invalid