简体   繁体   中英

How to call a servlet method from a JSP?

I have the following method in my Servlet.

private String process(HttpServletRequest arg0, HttpServletResponse arg1) {
    return ("a key");
} 

protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
    process(arg0, arg1);
}

In web.xml the following code is added

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>iusa.ubicacel.actions.map.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

In inicio.jsp the following is added

<script type="text/javascript" src="<%=request.getContextPath()%>/MyServlet"></script>

In the src tag above I want to add google map api url(which I will retrieve from the database in the servlet) from the process method in the MyServlet.I understand from the comments that my approach is wrong.Can anyone please tell me how to do it correctly with only this jsp and servlet.

A best practice for writing servlets with JSP is to follow the MVC pattern: your servlet will be the controller, the JSP is the view, while the model will consist of your domain objects which are passed from the servlet to the JSP page via request attributes.

I don't think that what you have right now is entirely wrong. But it's only suited for a special scenario where you will need to generate all your javascript code from a servlet (and this is hardly ever a true requirement). Assuming though that this is a true requirement in your case (perhaps you read the whole javascript content from a database), it's OK to define a servlet that renders the JS content (and perhaps map it as /main.js or something, to make the dynamic generation transparent for the JSP page).

Most likely, you need only a bunch of small items to be dynamically generated at runtime (like your google maps url, API key or whatever you store in your database). If this is the case, then your JavaScript code can be statically defined in a .js file and allow initialization with some constructor arguments (or whatever).

In this setup, your servlet will read the url from the database, will pass it to the view by calling request.setAttribute("googleMapsUrl", url) and then call requestDispatcher.forward(...) to pass control to the JSP.

In the JSP, you'll now need to include your static script with src and then you can have another script tag to initialize your code based on dynamic values bound to your request:

<c:url value="/static.js" var="scriptUrl"/>
<script type="text/javascript" src="${scriptUrl}"></script>
<script type="text/javascript">
    // let's assume your static script defines an object called `MyGoogleMapsDriver`...
    var googleMapsDriver = new MyGoogleMapsDriver('${googleMapsUrl}');
</script>

I hope this helps.

You dont need that, you should access to data so :

Save data from Servlet -> request.setAttribute( "MyObject", data);

After in JSP you load data that need -> request.getAttribute( "MyObject" ) ;

Sorry my english, good luck.

Note : I don't recommend to do this, but this is the direct answer to the question. For more information, take a look at the comments.


If you just ' want to add the string returned from the process method ' you need to do the following:

  1. Make your method public and static .
  2. Then write the following scriptlet: <%= MyServletName.process(request, response); %> <%= MyServletName.process(request, response); %> . This will output the result of the process method.

At the end you will have the following:

<script src="<%= MyServletName.process(request, response); %>"></script>

The variables request and response are available in this scope.

Important : The thing you are trying to achieve this way looks like a bad design . For various reason commented in this answer. Check the comments made by @LuiggiMendoza and @DaveNewton.

Here are some points to take in account:

  1. Writing scriplet is easy but is not recommended by any mean. See: How to avoid Java code in JSP files? .

  2. Invoking a Servlet method from JSP is bad design . Servlet methods are designed to handle HTTP methods. They were not designed to handle specific situation.

  3. The thing you are trying to do is an anti-pattern , you are not separating concerns. A JSP page should be a view that structure and render information. That information should be pre-processed.

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