简体   繁体   中英

Liferay : How to call a Servlet from a JSP Page

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 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.

yyy - Here's the answer to your comment:

In your JSP page you'll need to add the following for each of the actions you want that portlet to perform:

<portlet:actionURL var="addUserURL">
<portlet:param name="<%= ActionRequest.ACTION_NAME %>" value="addUser" />
</portlet:actionURL>

<form method="post" action="<%= addUserURL %>">

Then in your com.test.Greeting portlet you'd have for each of these:

public void addUser (ActionRequest actionRequest, ActionResponse actionResponse) {}

Does this answer your question?

Also it's usually best to start a new question rather than adding a comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM