将对象从servlet传递到jsp

时间:2014-07-03 11:01:09

标签: hibernate jsp servlets

我是初学者,我使用jsp servlet和hibernate,我想将对象从servlet传递给jsp

基本上我想用id输出当前的Faculty及其各自的名字。

这是我的编程:

package com.college;

import com.college.faculty.Faculty;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class FacultyExtract extends HttpServlet {



SessionFactory factory;
public void init(ServletConfig config ) throws ServletException
{
factory = new Configuration().configure().buildSessionFactory();
System.out.println("Factory has been created");
}

public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException
{
//HttpSession obses = request.getSession();
int pageIndex=0;
int totalNumberOfRecords=0;
int numberOfRecordsPerPage=4;

String sPageIndex= request.getParameter("pageIndex");

if(sPageIndex==null)
{

pageIndex=1;

}
else
{
pageIndex = Integer.parseInt(sPageIndex);
}

Session ses = factory.openSession();
int s= ( pageIndex*numberOfRecordsPerPage)-numberOfRecordsPerPage;

Criteria crit = ses.createCriteria(Faculty.class);
crit.setFirstResult(s);
crit.setMaxResults(numberOfRecordsPerPage);

List l= crit.list();
Iterator it =l.iterator();


for (int i=1;i>=6;i++)
{
        Faculty m = (Faculty)it.next();
        if( i==1 )
        {
            Faculty a1 = (Faculty) m ; 
            request.setAttribute("F1",a1);

        } 
        if( i==2 )
        {  Faculty a2 = (Faculty) m ;   
        request.setAttribute("F2",a2);
        } 
        if( i==3 )
        {  Faculty a3 = (Faculty) m ;   
        request.setAttribute("F3",a3);
        } 
        if( i==4 )
        {

            Faculty a4 = (Faculty) m ;
        request.setAttribute("F4",a4);
        } 
        if( i==5 )
        {  

            Faculty a5 = (Faculty) m ;   
                        request.setAttribute("F5",a5);
        } 
        if( i==6 )
        {  

            Faculty a6 = (Faculty) m ; 
             request.setAttribute("F6",a6);
        } 
    }

RequestDispatcher dispatcher = request.getRequestDispatcher("Faculty.jsp");

dispatcher.forward(request , response);

}

}

Faculty.jsp

<%@page import="com.college.faculty.Faculty"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>

    <% 
       //HttpSession obses=request.getSession();

        Faculty fac =(Faculty) request.getAttribute("f1");  

    //  System.out.println(fac.getExpDetail());  
        //String f2 = (String)fac.getQulification();

    %>

    <h1>Hello World!</h1>

    <%=fac.getExpDetail()%>


</body>
</html>

错误代码:

HTTP Status 500 - java.lang.NullPointerException

type Exception report

message java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling          this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:5    49)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
com.college.FacultyExtract.service(FacultyExtract.java:116)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause

java.lang.NullPointerException
org.apache.jsp.Faculty_jsp._jspService(Faculty_jsp.java:90)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
com.college.FacultyExtract.service(FacultyExtract.java:116)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.3   logs.

faculty.java

package com.college.faculty;

public class Faculty {
private int fid;
private String fname;
private String qulification;
private String expDetail;

public void setFacQul(int fid,String fname,String qulification,String expDetail)
{
    this.fid = fid;
    this.fname = fname;
    this.qulification = qulification;
    this.expDetail = expDetail;
}

public int getFid() {
    return fid;
}
public void setFid(int fid) {
    this.fid = fid;
}
public String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}
public String getQulification() {
    return qulification;
}
public void setQulification(String qulification) {
    this.qulification = qulification;
}
public String getExpDetail() {
    return expDetail;
}
public void setExpDetail(String expDetail) {
    this.expDetail = expDetail;
}




}

1 个答案:

答案 0 :(得分:1)

您在servlet中使用大写“F1”,在jsp“f1”中使用小写。它必须是一样的。最好定义具有常量的接口,例如属性名称区分大小写。

//servlet    
request.setAttribute("F1",a1); //upper
//jsp
Faculty fac =(Faculty) request.getAttribute("f1");   // lower

还有Si Kelly指出的错误:

“你的循环中有错误:for(int i = 1; i&gt; = 6; i ++) - 这永远不会进入因为我从来没有&gt; = 6 - Si Kelly”

应该是(int i = 1; i.hasNext(); i ++)或者如果集合中的元素少于6,则会有异常。