当前位置: 技术问答>java相关
紧急求救:在做Model II的例子时候遇到了问题,(在线等待,立刻给分)
来源: 互联网 发布时间:2015-05-28
本文导语: 欲做一留言的小程序,采用jsp+servlet+ bean模式: 现在只想从数据库中的摘要信息表(yang_digest)显示摘要信息: 控制器servlet如下:(编译无误) import javax.servlet.*; import javax.servlet.http.*; public class Controller extends ...
欲做一留言的小程序,采用jsp+servlet+ bean模式:
现在只想从数据库中的摘要信息表(yang_digest)显示摘要信息:
控制器servlet如下:(编译无误)
import javax.servlet.*;
import javax.servlet.http.*;
public class Controller extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void destroy()
{
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,java.io.IOException
{
String action = request.getParameter("action") ;
String dispatch ="/servlet/" ;
if(action == null)
dispatch += "Digest" ;
else if(action.equals("message"))
dispatch += "Message" ;
else if(action.equals("author"))
dispatch += "Author" ;
RequestDispatcher dispatcher = request.getRequestDispatcher(dispatch) ;
dispatcher.forward(request, response) ;
}
public String getServletInfo()
{
return "Controlling Servlet for Model2 Demonstration";
}
}
摘要信息表的实体 DigestMessageBean如下(编译无误)
package testb;
import java.beans.*;
import java.io.Serializable ;
public class DigestMessageBean implements Serializable
{
private int id ;
private String title ;
private String author;
private int ofmessageid;
public DigestMessageBean()
{
}
public int getId()
{
return id ;
}
public String getTitle()
{
return title ;
}
public String getAuthor()
{
return author ;
}
public int getOfmessageid()
{
return ofmessageid;
}
public void setId(int value)
{
id = value ;
}
public void setTitle(String value)
{
title = value ;
}
public void setAuthor(String value)
{
author = value ;
}
public void setOfmessageid(int value)
{
ofmessageid=value;
}
}
Controller 转向的的servlet如下:
import testb.DigestMessageBean;
import testb.DBBean;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Digest extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void destroy()
{
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,java.io.IOException
{
try
{
String querySQL="select id,title,author,ofmessageid from yang_digest";
DBBean db=new DBBean();
ResultSet rs = db.executeQuery(querySQL) ;
ResultSetMetaData rsmd = rs.getMetaData() ;
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
Vector digest = new Vector() ;
while(rs.next())
{
DigestMessageBean dmb = new DigestMessageBean() ;
dmb.setId(rs.getInt(1)) ;
dmb.setTitle(rs.getString(2)) ;
dmb.setAuthor(rs.getString(3)) ;
dmb.setOfmessageid(rs.getInt(4));
digest.addElement(dmb) ;
}
rs.close();
request.setAttribute("digest",digest) ;
String dispatch ="/viewbean.jsp" ;
RequestDispatcher dispatcher = request.getRequestDispatcher(dispatch) ;
dispatcher.forward(request,response) ;
//out.println("");
//out.println("yyyy");
//out.println("");
//out.println("hello yangping");
//out.println("");
}
catch(SQLException ex)
{
}
}
}
进行显示的jsp页如下:
Servlet-JSP Page
Message Digest
Click on the message id to view a specific message
ID
Title
Author
不知道大家看没看明白?
tomcat4.0 web server.
运行http://speed001:8080/test/servlet/Controller 出错:
org.apache.jasper.JasperException: Unable to compile class for JSP
C:Program FilesApache Tomcat 4.0worklocalhosttestviewbean$jsp.java:145: method 的不兼容类型。从 java.lang.Throwable 到 java.lang.Exception 的转换需为显式强制类型转换。
if (pageContext != null) pageContext.handlePageException(t);
请问如何解决?????
现在只想从数据库中的摘要信息表(yang_digest)显示摘要信息:
控制器servlet如下:(编译无误)
import javax.servlet.*;
import javax.servlet.http.*;
public class Controller extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void destroy()
{
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,java.io.IOException
{
String action = request.getParameter("action") ;
String dispatch ="/servlet/" ;
if(action == null)
dispatch += "Digest" ;
else if(action.equals("message"))
dispatch += "Message" ;
else if(action.equals("author"))
dispatch += "Author" ;
RequestDispatcher dispatcher = request.getRequestDispatcher(dispatch) ;
dispatcher.forward(request, response) ;
}
public String getServletInfo()
{
return "Controlling Servlet for Model2 Demonstration";
}
}
摘要信息表的实体 DigestMessageBean如下(编译无误)
package testb;
import java.beans.*;
import java.io.Serializable ;
public class DigestMessageBean implements Serializable
{
private int id ;
private String title ;
private String author;
private int ofmessageid;
public DigestMessageBean()
{
}
public int getId()
{
return id ;
}
public String getTitle()
{
return title ;
}
public String getAuthor()
{
return author ;
}
public int getOfmessageid()
{
return ofmessageid;
}
public void setId(int value)
{
id = value ;
}
public void setTitle(String value)
{
title = value ;
}
public void setAuthor(String value)
{
author = value ;
}
public void setOfmessageid(int value)
{
ofmessageid=value;
}
}
Controller 转向的的servlet如下:
import testb.DigestMessageBean;
import testb.DBBean;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Digest extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void destroy()
{
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,java.io.IOException
{
try
{
String querySQL="select id,title,author,ofmessageid from yang_digest";
DBBean db=new DBBean();
ResultSet rs = db.executeQuery(querySQL) ;
ResultSetMetaData rsmd = rs.getMetaData() ;
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
Vector digest = new Vector() ;
while(rs.next())
{
DigestMessageBean dmb = new DigestMessageBean() ;
dmb.setId(rs.getInt(1)) ;
dmb.setTitle(rs.getString(2)) ;
dmb.setAuthor(rs.getString(3)) ;
dmb.setOfmessageid(rs.getInt(4));
digest.addElement(dmb) ;
}
rs.close();
request.setAttribute("digest",digest) ;
String dispatch ="/viewbean.jsp" ;
RequestDispatcher dispatcher = request.getRequestDispatcher(dispatch) ;
dispatcher.forward(request,response) ;
//out.println("");
//out.println("yyyy");
//out.println("");
//out.println("hello yangping");
//out.println("");
}
catch(SQLException ex)
{
}
}
}
进行显示的jsp页如下:
Servlet-JSP Page
Message Digest
Click on the message id to view a specific message
ID
Title
Author
不知道大家看没看明白?
tomcat4.0 web server.
运行http://speed001:8080/test/servlet/Controller 出错:
org.apache.jasper.JasperException: Unable to compile class for JSP
C:Program FilesApache Tomcat 4.0worklocalhosttestviewbean$jsp.java:145: method 的不兼容类型。从 java.lang.Throwable 到 java.lang.Exception 的转换需为显式强制类型转换。
if (pageContext != null) pageContext.handlePageException(t);
请问如何解决?????
|
这不是你的程序的错误,是Tomcat路径的问题。在老的servlet api中,handlePageException只能用Exception当参数,在新的api中,handlePageException也能用Throwable当参数。jsp转译中会加一个try/catch( Throwable t),如果你用了老的servlet.jar,就会出现上面的错误。所以好好检查一下路径,以及Tomcat的common/lib和你的WEB-INF/lib等地方,看看有没有老的servlet.jar。
|
把 Throwable t 改成 Exception e