HTTP GET
You can use a hyper link for a GET request.
You can add parameters as query information.
HTTP POST
Parameters are contained within the body.
It is said that HTTP POST is safer than HTTP GET.
Transfering a request from a HTML file to a Servlet
Hyper link(GET)
<a href="/scwcd/Test?information=Hello everyone">Test</a>
The result will be "Hello everyone"
Using a form (GET)
<form action="/scwcd/Test">
<input type="text" name="information" size="30" maxlength="15" /><br />
<input type="submit" value="Send"/>
</form>
The method attribute of a form is "get" by default.
What you put in the form will be displayed in a Servlet.
In the example below, a doGet method is not defined in a Servlet, but a service method and a doPost method is defined.
In this case, the doPost metod works because every request goes through a service method.
The overridden service method calls the doPost method, so Test.java works.
Test.java
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.*;
import javax.servlet.http.*;
public class Test extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse res) {
try {
doPost((HttpServletRequest)req, (HttpServletResponse)res);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("<html><body>");
out.println(req.getParameter("information"));
out.println("</body></html>");
}
}