Reputation: 3224
I'm wondering is possible to integrate custom servlet logic with .jsp template view. For example I have the following servlet:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = "Mark";
}
}
and I want to place name
variable inside jsp file(new.jsp) like:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>New</title>
</head>
<body>
<%= name %>
</body>
</html>
My web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<jsp-file>/new.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/new</url-pattern>
</servlet-mapping>
I don't want to put name
in request.
Any help?
UPDATE
Great thanks, but I'm still having a trouble. Firstly, I updated my servlet:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = "Mark";
request.setAttribute("name", name);
request.getRequestDispatcher("/WEB-INF/new.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = "Mark";
request.setAttribute("name", name);
request.getRequestDispatcher("/WEB-INF/new.jsp").forward(request, response);
}
}
I also changed my view:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>New</title>
</head>
<body>
${name}
</body>
</html>
But when I use ${name}
there's nothing displayed. I thought that I should import any jstl, but unfortunately if I use <%= request.getAttribute("name") %>
I'm getting null
.
UPDATE 2 Finally solved! It was my fault, I forgot set
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
</servlet>
Upvotes: 3
Views: 8176
Reputation: 6490
If you don't want to put "name" in the request, then the only way is to use scriptlet within the JSP code. This is not a clean or standards based solution though.
REPEAT ## THIS IS NOT A CLEAN SOLUTION ## USE request to store the name as others have pointed out.
So for example
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>New</title>
</head>
<body>
<% String name = "Mark" %> <%-- PUT THE name IN HERE --%>
<%= name %>
</body>
</html>
But what is the reason why you don't want to put name in the request? That was why servlets are there for.
Upvotes: 0
Reputation: 1108537
You need to implement the doGet()
method instead. A normal HTTP request (clicking a link, a bookmark or entering the URL straight in browser address bar) defaults to GET
method.
In order to make objects available in JSP in a preprocessing servlet, you need to set it as an attribute in request, session or application scope. Finally you need to forward the request/response to the JSP so that it can be displayed.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "Mark";
request.setAttribute("name", name);
request.getRequestDispatcher("/WEB-INF/new.jsp").forward(request, response);
}
If you fix the servlet mapping as follows
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/new</url-pattern>
</servlet-mapping>
then you can just invoke the servlet by http://localhost:8080/contextname/new. In the forwarded JSP you can access the name
just by EL
${name}
Note that the JSP is placed in /WEB-INF
folder in order to avoid direct access without the preprocessing servlet being called by entering the JSP URL in browser address bar instead.
Upvotes: 5
Reputation: 11817
You can set name
as a request attribute and get it in the JSP.
For example, in your servlet, you can do:
request.setAttribute("name", name);
and get it using:
<%= request.getAttribute("name") %>
or better yet, use EL:
${requestScope.name}
Upvotes: 1