2016-11-29 543 views
-1

我尝试在Servlet中将文本保存为属性并将其转发到HTML页面。然后需要在Html页面主体中显示它。 帮我发送数据作为对HTML的响应,然后帮助显示HTML页面中的值。 在JSP中工作正常。但我需要将响应发送到html页面并显示它。如何从Servlet中获取参数到Html页面?

Here i am using Request dispatcher for send the request and response to html page. 
but i am not clear with how to display it in html.Help me tp solve. 

thanks 

//NewServlet.java 

public class NewServlet extends HttpServlet { 
protected void processRequest(HttpServletRequest request,HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    request.setAttribute("Cricket", "Sachin"); 
    RequestDispatcher rd = request.getRequestDispatcher("index.html"); 
    rd.forward(request, response); 
} 

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
    } 
} 

//index.html

enter image description here

+0

'index.html'不是jsp –

+0

当你有jsp的时候使用JSTL –

+0

我能想到的唯一方法就是预处理html,并用你想显示的任何东西替换“TODO写内容”。 –

回答

-1

如果index.html文件在另一台服务器上,你并不需要,因为你需要做的另一个请求使用要求的属性,而servlet API不提供你所需要的。

例如,您可以通过使用HttpURLConnection来获取文件的内容。然后,您需要处理该内容以插入数据,然后将结果写入servet响应。

相关问题