Hello,
I'm trying to understand a very simple example of using html + servlets using Eclipse EE.
I have an index.html :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ze title!</title>
</head>
<body>
<form action="MyServlet" method = post>
<input type= text name = text>
<input type = submit value="Submit">
</form>
</body>
</html>
And a servlet named MyServlet:
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String s=(String) request.getParameter("text");
out.print("<html>");
out.print("<head><title>Added title</title></head>");
out.print("<body>");
out.print("input= "+s+" this is the GET method");
out.print("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String s=(String) request.getAttribute("text");
out.print("input= "+s+" this is the POST method");
}
}
So I have a doGet and a doPost method. If I use:
<form action="MyServlet" method = post>
in my index.html, the Eclipse browser still executes the GET method, while Opera uses the POST method. Why is this?
Furthermore, can I add more methods in my servlet and call them from my html?
<form action="MyServlet" method = FooMethod>
Now that I've put it in writing, it does look pointless. In the tutorials I've found, I've only seen adding more methods but calling them from within the GET/POST methods, not directly.
Thanks.