Passing value from servlet to html

I have a servlet that processes some content from the Internet and generates a String value. I need to display this String value on an html page in a table tag.

How to pass this string value from a servlet using the setAttribute method and getrequestdispatcher method?

Thanks Abhishek S

+3
source share
5 answers

You can transfer data from a servlet to JSP (not HTML) using the query ahead and by setting the data as an attribute in the query, and then in JSP you can visualize this data to generate HTML


Cm

+3
source

In yours, Servletset the data as an attribute in request:

RequestDispatcher dispatcher = request.getRequestDispatcher("yourJspPage.jsp");
request.setAttribute("Name", "Temp"); // set your String value in the attribute
dispatcher.forward( request, response );

jsp request :

<table>
    <tr>
        <td><%=request.getAttribute("Name")%></td>
    </tr>
</table>

, !

+6

PrintWriter, HTML.
response - HttpServletResponse doGet doPost.

response.setContentType("text/html");  
PrintWriter out = response.getWriter();  
out.println("<html-code>")

,

out.println("<html><body><table>...your code...</table></body></html>");

HTML.

0

, ajax get html jquery. html script

$.get('HelloServlet', {a:'abc',b:'abc'}, function (data) {  
   alert(data);  
});

Servlet

String str = "abc";
PrintWriter out = response.getWriter();  
out.write(str);

, "str" alert "data".

0

You can do this by passing the value of the servlet as HTML-JavaScript content, and then access that tag in the script tag.

You can try the following: In the Servlet method

PrintWriter out = response.getWriter (); out.print ("var xyz = 20;");

In the HTML page Inside the script tag:

var abc = xyz;

But you will need to execute the servlet on the HTML page. In tomcat, if you have a servlet mapping, just type:

"<\ script src =" / servlet -name "> </script>

0
source

All Articles