This is my first Portlet. I am not getting values inside my servlet. Please, see the program. Inside my custom portlet Java class doView() method, I show a JSP page
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(viewJSP, renderRequest, renderResponse);
}
Inside the view.jsp page, I refer to a servlet to receive the values:
<form action="formServlet" method="post">
<h1>Please Login</h1>
Login: <input type="text" name="login"><br>
Password: <input type="password" name="password"><br>
<input type=submit value="Login">
</form>
Inside web.xml file:
<servlet>
<servlet-name>formServlet</servlet-name>
<servlet-class>FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>formServlet</servlet-name>
<url-pattern>formServlet</url-pattern>
</servlet-mapping>
Inside my servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = (String)request.getParameter("login");
System.out.println("The Name is "+name);
}
But I don’t know why the servlet is not being called.
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 thedoPost()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 beinstead of
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
portlettaglib in your JSP:Now, replace the
actionof 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: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 returnvoidand receive two parameters, of the timejavax.portlet.ActionRequestandjavax.portlet.ActionResponse. This is an example of an emptyprocessAction():When a request to an action URL (as the one generated by
<portlet:actionURL />) is sent to the server, it is firstly processed by theprocessAction()method and then bydoView(). Therefore, the code you would write in your servlet should be put in yourprocessAction(). The result should be then:Try it and you will see that it will work well.