谁知道servlet的RequestDisPatcher的用法:帮我看看程序

shangyongjun 2004-10-20 09:02:36
谁知道servlet的RequestDisPatcher的用法:帮我看看程序:

RequestDisPatcher requestDisPatcher = request.getRequestDispatcher("/jsp_servlet_javabean/viewMessage.jsp");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
上面画线的路径应该怎么表示
谁能鲜细的给我说说???
程序的源代码如下:

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import java.io.*;
public class ViewMessageServlet extends HttpServlet
{
private Connection conn;
//接受Get的请求。执行数据库操作,保存到request中,
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
Collection ret =new ArrayList();
try
{
Statement stm = conn.createStatement();
ResultSet result = stm.executeQuery("select count(*)from message");
int message_count = 0;
if(result.next())
{
message_count = result.getInt(1);
result.close();
}
if(message_count>0)
{
result = stm.executeQuery("select * from message order by time desc");
while(result.next())
{
String title = result.getString("title");
String name = result.getString("name");
String mail = result.getString("mail");
String content = result.getString("content");
java.sql.Date Date = result.getDate("time");
MessageVO message = new MessageVO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setDate(Date);
message.setEmail(mail);
ret.add(message);

}
result.close();
stm.close();

}
//保存执行结果,把试图派发到目标
request.setAttribute("message",ret);
RequestDisPatcher requestDisPatcher = request.getRequestDispatcher("/jsp_servlet_javabean/viewMessage.jsp");

}catch(Exception e)
{
e.printStackTrace();
}


}
//初始化到数据库的连接
public ViewMessageServlet()
{
String classname = "sun.jdbc.odbc.JdbcOdbcDriver";
String strdb = "jdbc:odbc:shang";
String name = "";
String pwd = "";
try
{
Class.forName(classname);
conn = DriverManager.getConnection(strdb,name,pwd);


}catch(Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
doGet(request,response);
}
}
...全文
144 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shangyongjun 2004-10-20
to sxfzr(寒)
用你的方法搞定了
谢谢你
还有谢谢楼上各位的帮助
回复
shangyongjun 2004-10-20
我已经加上了requestDisPatcher.forward(request,response);
但是还是不行
是不是用这个方法要导入什么类呀?
回复
tiandiqing 2004-10-20
这样看行不行?

request.getRequestDispatcher(("/jsp_servlet_javabean/viewMessage.jsp").forward(request,response);
回复
sxfzr 2004-10-20
更正一下试试:getServletConfig().getServletContext().getRequestDispatcher("/jsp_servlet_javabean/viewMessage.jsp").forward(request,response);
回复
sxfzr 2004-10-20
jsp文件保存绝对路径除去与servlet字节码文件相同的路径后的剩余部分就可以
RequestDisPatcher requestDisPatcher = request.getRequestDispatcher("/jsp_servlet_javabean/viewMessage.jsp");后加requestDisPatcher.forward(request,response);

回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告