I've seen servlets examples, they're something like this:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
...
}
My question is, instead of the code, can I return an HTML page? I mean, something like this:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
SHOW(FILE.HTML);
}
Thanks!!! ;)
First create a PrintWriter object, which will produce the output on HTML page. Here response is HttpServletResponse object from doGet or doPost method. out. println("<html><body><table>...
The most common use for a servlet is to extend a web server by providing dynamic web content. Web servers display documents written in HyperText Markup Language (HTML) and respond to user requests using the HyperText Transfer Protocol (HTTP). HTTP is the protocol for moving hypertext files across the internet.
Reading Form Data using Servlet getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getParameterNames() − Call this method if you want a complete list of all parameters in the current request.
There are a few different ways you could do this:
Forward the servlet to the path where the HTML file is located. Something like:
RequestDispatcher rd = request.getRequestDispatcher("something.html");
rd.forward(request, response);
Send a redirect to the URL where the HTML is located. Something like:
response.sendRedirect("something.html");
Read in the contents of the HTML file and then write out the contents of the HTML file to the servlet's PrintWriter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With