How to return an html document from java servlet?
You either print out the HTML from the Servlet itself (deprecated)
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>My HTML Body</h1>");
out.println("</body></html>");
or, dispatch to an existing resource (servlet, jsp etc.) (called forwarding to a view) (preferred)
RequestDispatcher view = request.getRequestDispatcher("html/mypage.html");
view.forward(request, response);
The existing resource that you need your current HTTP request to get forwarded to does not need to be special in any way i.e. it's written just like any other Servlet or JSP; the container handles the forwarding part seamlessly.
Just make sure you provide the correct path to the resource. For example, for a servlet the RequestDispatcher
would need the correct URL pattern (as specified in your web.xml)
RequestDispatcher view = request.getRequestDispatcher("/url/pattern/of/servlet");
Also, note that the a RequestDispatcher
can be retrieved from both ServletRequest
and ServletContext
with the difference that the former can take a relative path as well.
Reference:
http://docs.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html
Sample Code
public class BlotServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// we do not set content type, headers, cookies etc.
// resp.setContentType("text/html"); // while redirecting as
// it would most likely result in an IllegalStateException
// "/" is relative to the context root (your web-app name)
RequestDispatcher view = req.getRequestDispatcher("/path/to/file.html");
// don't add your web-app name to the path
view.forward(req, resp);
}
}
Set an html page as servlet response
You just forward
request to jsp page
String nextJSP = "/yourJsp.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
You updated question from jsp to html
in that case you just need to redirect user to goto HTML, since src/main/webapp is in public web space it would be available to user directly
response.sendRedirect("/yourHtml.html")
or you still can forward request to html
String nextHTML = "/yourHtml.html";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextHTML);
dispatcher.forward(request, response);
Servlet response in particular part of HTML
Try first put in output stream part of html before place where you need input your data:
pwritter.print("<form name="input" action="ExamServlet" method="get">" +
"<table>"+
"<tr>"+
"<th>Nr.</th>"+
"<th>");
then put there your data:
pwritter.print("here i am ");
then put what is left from static html.
pwritter.print("</th>"+
"</tr>"+
"<tr>"+
"<td>3+1=4?</td>"+
"<input type="submit" value="Einreichen">"+
"</form>");
and then do dis.forward(request, response);
How to put result sent by servlet into html input box?
In short.
Create the form in the doGet()
method of the servlet. In the action
attribute, specify the name of the servlet in the method
attribute, specify POST
.
...
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(...) // form here
...
You can use *.html
file instead of creating the form in the servlet.
Get the passed parameters in the doPost()
method of the servlet.
...
request.getParameter("First");
request.getParameter("Second");
request.getParameter("Sum");
...
Perform the necessary checks, generate the form with the filled fields.
Then call the servlet you will see the blank form. Write values in the fields and submit the form. You will see the filled form.
Although it is better to use JSP in this case - What is the difference between JSF, Servlet and JSP?
how to pass servlet response to html text using javascript
Whatever you will print i.e : System.out.println(indDate);
this will return back as a response in ajax call.Also ,you cannot forward your page from servlet as you are using ajax here you need to sent back something and from there you can forward.So,remove these request.getRequestDispatcher("upload.jsp").forward(request, response);
lines from servlet .Also your ajax call should look like below :
function strSO(){
var salesOrder=$("#sOrder option:selected").val();
$.ajax({
type:'get',
url:'getValues',
data:'salesOrder='+salesOrder,
success:function(data){
console.log("success");
document.getElementById("indDate").value=data;//indDate will be asign to textbox
//you can write redirect code here
}
});
}
Related Topics
Detect When an Html5 Video Finishes
Curve Bottom Side of the Div to the Inside With Css
How to Add a New Line in Textarea Element
How to Force a Web Browser Not to Cache Images
What Is the Correct Value For the Disabled Attribute
Putting Images With Options in a Dropdown List
Centered Elements Inside a Flex Container Are Growing and Overflowing Beyond Top
Html Encoding Issues - "Â" Character Showing Up Instead of "&Nbsp;"
Does Opacity:0 Have Exactly the Same Effect as Visibility:Hidden
Change Div Order With CSS Depending on Device-Width
Css Content Generation Before or After 'Input' Elements