NOTE: This is an answer to a somewhat complicated question. If you are trying to learn the basics of portlet creation, I posted a better answer in another question.
You are submitting a form using the POST method but your servlet just implements doGet()
, which serves the GET method. You should either submit your form using GET or implement the doPost()
method (which would be preferable in other situations).
Also, it is necessary to precede the <url-pattern>
content by a slash if it is an absolute pattern. That is, it should be
<url-pattern>/formServlet</url-pattern>
instead of
<url-pattern>formServlet</url-pattern>
That said, forget servlets now!
You are doing it in one of the worst ways. It is really a bad idea to write a portlet which calls a servlet. After a long time working with Liferay I can imagine situations where it would be more or less reasonable, but it is not here and will not be most of the times.
So, what should you do? You should submit your form to an action URL. To do it, first include the portlet
taglib in your JSP:
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
Now, replace the action
of your form by the <portlet:actionURL />
. This tag will be replaced by a special URL generated by the portal. Also, precede each input name with the tag <portlet:namespace />
; your <input type="text" name="login">
should become <input type="text" name="<portlet:namespace />login">
then. This tag will be replaced by a string which is associated with only your portlet; since you can have a lot of portlets in a page, each input should specify from what portlet it comes from. This is the final result:
<form action="<portlet:actionURL />" method="post">
<h1>Please Login</h1>
Login: <input type="text" name="<portlet:namespace />login"><br>
Password: <input type="password" name="<portlet:namespace />password"><br>
<input type=submit value="Login">
</form>
Now you are going to submit your data correctly - but how to get the submitted data? It certainly is not necessary to use a servlet! Instead, add to your custom portlet class a method called processAction()
. This method should return void
and receive two parameters, of the time javax.portlet.ActionRequest
and javax.portlet.ActionResponse
. This is an example of an empty processAction()
:
public void processAction(ActionRequest request, ActionResponse response) {
// Nothing to be done for now.
}
When a request to an action URL (as the one generated by <portlet:actionURL />
) is sent to the server, it is firstly processed by the processAction()
method and then by doView()
. Therefore, the code you would write in your servlet should be put in your processAction()
. The result should be then:
public void processAction(ActionRequest request, ActionResponse response) {
String name = (String)request.getParameter("login");
System.out.println("The Name is "+name);
}
Try it and you will see that it will work well.