从servlet调用jsp

问题描述 投票:4回答:6

我正在调用一个JSP,displayItems.jsp来自servlet,DataPortal.java。首先,我尝试使用像这样的RequestDispatcher这样做,

String url = "/displayItems.jsp";
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher(toDo);
dispatcher.forward(req, res);

嗯...控件确实进入了JSP页面,但是它打印了JSP文件的全部内容(包括标签和所有内容),而不是显示为网页。接下来我试图通过使用response.sendRedirect(url);实现这一点,这次它给了我一个空页面。我在这做错了什么? JSP就像这样,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />        
<meta http-equiv="Content-Style-Type" content="text/css" />
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.js" type="text/javascript"></script>
</head>
    <body>
    <div>i am in display category</div>
    </body>
</html>

任何帮助表示赞赏。

jsp servlets redirect forward
6个回答
3
投票

试试这个

RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/displayItems.jsp");
RequetsDispatcherObj.forward(request, response);

1
投票

问题已经解决了。这是如此:我有一个DispatchServlet调用DataPortal,而DataPortal又调用displayItems.jsp。 dispatcher.forward在DataPortal中工作的原因是因为我在DispatchServlet中执行dispatcher.include来调用DataPortal。当我把它改为前进时,事情就开始起作用了。谢谢你们,感谢您的回复。


0
投票

怎么样dispatcher.include(req, res)?这是你想从servlet调用一个jsp。


0
投票

转发只会将请求转发到下一页,因为sendRedirect将首先返回到生成它的页面并重定向到下一页


0
投票
RequestDispatcher dispatcher = getRequestDispatcher("URL to jsp");
dispatcher.forward(request, response);

0
投票

RequestDispatcher dispatcher = getRequestDispatcher(request.getContextPath()+“/”); dispatcher.forward(请求,响应);

© www.soinside.com 2019 - 2024. All rights reserved.