2014-09-25 377 views
2

我正在开发一个使用servlet(v3.0)和jetty v9的web服务器。我必须提供HTML页面,但在此之前,我需要根据布尔变量的值为页面上的几个元素修改CSS内联样式。如何将值从servlet传递到html页面

我一直在寻找JSP教程和例子的年龄,我不觉得我更接近于搞清楚。为了简化它,这就是我想要做的事:

page.jsp:(中/ WAR/HTML)

<html> 
    <head>My Page</head> 
    <body> 
     <p <% style="display:none" if variable is true %>></p> 
    </body> 
</html> 

GetPage.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(???); 
    } 
} 

我一直在使用Java多年,但从未使用过JSP。我原以为这是JSP 101,但我不明白它是如何工作的。另外,我的真实用例与上面的示例并不相距太远。 JSP是否为此目的过度杀伤,如果是这样,是否有更好的选择?

+1

http://stackoverflow.com/questions/54的可能的复制14600/pass-data-from-java-servlet-to-jsp – mkobit 2014-09-25 18:26:20

回答

4

没有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>"); 

或者与Jquery Ajax(无JSP),您可以发送Ajax请求到Servlet,并从你的servlet响应。这样你就不需要在你的servlet中连接你的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> 

用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

<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"); 
} 
+0

感谢您的详细回复。但是,我绝对想要使用JSP(或任何其他模板引擎)的路线。我了解JSP中实际的替换语法是怎么回事,但它是我没有得到的服务器部分。我不想从servlet写HTML,HTML太多了。我遇到的问题是如何从servlet中加载JSP页面,将该变量传递给某个JSP解析对象(大概是),然后返回最终转换后的HTML页面。我正在研究Mike Kobit发布的dup问题的解决方案 – RTF 2014-09-26 18:05:37

+0

是的,该帖子有足够的信息让你开始。简要介绍如何将请求从servlet转发到jsp:首先设置要传入请求对象的对象,并使用getRequestDispatcher(url)定义目标jsp/servlet(也可以转发到其他servlet),最后转发请求。容器将负责解析等。另外,我用servlet代码更新了我的答案。有时我发现使用代码比解释更容易理解.. :) – Sas 2014-09-26 20:53:15

+0

框架如何知道它应该使用jsp引擎来处理页面?仅仅是因为jsp文件扩展名? – RTF 2014-09-26 22:23:38

2

的Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    request.setAttribute("attrib", true); 
    request.getRequestDispatcher("/page.jsp").include(request, response); 
} 

jsp的

${ attrib ? 'none': 'block'} 
相关问题