java 如何将值从 servlet 传递到 html 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26045368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass values from servlet to html page
提问by RTF
I'm developing a web server using servlet (v3.0) and jetty v9. I have to serve a HTML page but before I do, I need to modify the CSS inline-styles for a couple of elements on the page, dependent on the value of a boolean variable.
我正在使用 servlet (v3.0) 和 jetty v9 开发 Web 服务器。我必须提供一个 HTML 页面,但在此之前,我需要修改页面上几个元素的 CSS 内联样式,具体取决于布尔变量的值。
I've been looking at JSP tutorials and examples for ages, and I don't feel like I'm any closer to figuring it out. To simplify it, this is what I'm trying to do:
我一直在看 JSP 教程和示例多年,但我觉得我离弄明白它更近了。为了简化它,这就是我想要做的:
page.jsp: (in /WAR/html)
page.jsp:(在/WAR/html 中)
<html>
<head>My Page</head>
<body>
<p <% style="display:none" if variable is true %>></p>
</body>
</html>
GetPage.java:
获取页面.java:
public class GetPage extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
boolean decision = getDecision();
// get the JSP page and process the JSP with the boolean variable...
resp.setContentType("text/html");
resp.getWriter().print(???);
}
}
I've been using Java for years but never used JSP before. I would have thought this is JSP 101 but I don't understand how it works. Also, my real use case is not too far off that example above. Is JSP overkill for this purpose and if so, is there a better alternative?
我已经使用 Java 多年,但以前从未使用过 JSP。我原以为这是 JSP 101,但我不明白它是如何工作的。此外,我的实际用例与上面的示例相差不远。JSP 为此目的是否过大,如果是,是否有更好的选择?
回答by Sas
Without JSP, you can simply write the html from the servlet something like below:
如果没有 JSP,您可以简单地从 servlet 编写 html,如下所示:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
if(yourcondition){
<p style="display:none;"></p>
}else{
<p></p>
}
out.println("</body>");
out.println("</html>");
Alternatively with Jquery Ajax(without jsp) you can send ajax request to your servlet and get response from your servlet. This way you don't need to couple your html page writing in your servlet as shown above.
或者,使用Jquery Ajax(不带 jsp),您可以将 ajax 请求发送到您的 servlet 并从您的 servlet 获得响应。这样你就不需要在你的 servlet 中耦合你的 html 页面,如上所示。
HTML:
HTML:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("yourvalue");
}
<script>
$(document).ready(function(){
//sends ajax get request
$.get( "myServlet", function( data ) {
//Do your logic with the response
if(data == "myvalue"){
$("p").css("display", "none");
}
});
});
</script>
With jsp:
使用jsp:
Servlet
小服务程序
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
request.setAttribute("myvalue",val);
RequestDispatcher rd = sc.getRequestDispatcher("mypage.jsp");
rd.forward(req, res);
}
JSP
JSP
<p <c:if test="${your condition}">style="display: none;"</c:if>></p>
OR
或者
<p style="display: ${ var == 'myvalue' ? 'none': 'block'};"></p>
Jquery
查询
var myVal= '${val}'
if(myVal == "none"){
$("p").css("display", "none");
}
回答by mhmxs
Servlet
小服务程序
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("attrib", true);
request.getRequestDispatcher("/page.jsp").include(request, response);
}
Jsp
jsp
${ attrib ? 'none': 'block'}