Reputation: 361
I have a question about servlet/jsp/html
. I have a tab menu:
<ul class="nav nav-pills">
<li class="active"><a data-toggle="pill" href="#list">LIST OF PRODUCTS</a></li>
<li><a data-toggle="pill" href="#add">ADD PRODUCT</a></li>
</ul>
<div class="tab-content">
<div id="list" class="tab-pane fade in active">
<h3>LIST OF PRODUCTS</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div id="add" class="tab-pane fade">
<h3>ADD PRODUCT</h3>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
I would like to link a servlet
when I press a tab and consequently calling a jsp in the tab-content
, instead of :
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
My servlet
code :
@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
@EJB
private BDBeanLocal bDBean;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ArrayList<Products> list = new ArrayList<Products>();
list = bDBean.listaProdotti();
request.setAttribute("lista", list);
String arg = "/" + this.getServletName() + ".jsp";
RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(arg);
dispatcher.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
and this is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>servlet.NewServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>servlet.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
Upvotes: 0
Views: 1358
Reputation: 1332
Make the hyperlink have a URL that you have a servlet mapping defined for in the web.xml
file.
The servlet-mapping
element defines a mapping between a servlet and a URL pattern. The example below maps the servlet named myservlet to any URL that starts with /foo
:
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.foo.examples.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/anything/*</url-pattern>
</servlet-mapping>
For this example, a hyperlink such as <a href="/anything/test.html">Click Me</a>
would invoke the servlet.
by url-pattern
you said every request after /anything
should handled by myservlet Servlet
. and after that you use something(test.html) after your servlet
as your defined in Servlet
URL pattern
to send a request to myservlet
.
-----------
Another way to call a servlet is using parameters.
<a href="servletUrl?param=value">click</a>
I hope this helps you.
Upvotes: 1