2016-02-11 115 views
0

不知怎的,JavaBean是沒有得到在針對home.jsp拋出下面的錯誤稱爲我收到以下錯誤,而在JSP..The值指的JavaBean爲useBean的類屬性Controller.SampleBean無效

重度: Servlet.service()for servlet jsp拋出異常 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 
    } 

} 

} 
+0

類文件'SampleBean'應該是在'WEB-INF /班/ Controller'。 – Satya

+0

並應該有默認的構造函數,應該是'public' .. – Satya

回答

相關問題