What it says on the title. I'm trying to invoke a servlet to create a page when I click on a link. Attached below is the code.
HTML:
<html>
<head>
<title>NavBar</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="sheet.css">
</head>
<body>
<section class="ft_root">
<div class="ft">
<ul>
<li><a href="/entry/test.html"> 1 </a></li>
<li><a href="#"> 2 </a></li>
<li><a href="#"> 3 </a></li>
<li><a href="#"> 4 </a></li>
<li><a href="#"> 5 </a></li>
</ul>
</div>
</html>
Web.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>entry_page_gen</servlet-name>
<servlet-class>Servlets.entry_page_gen</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>entry_page_gen</servlet-name>
<url-pattern>/entry/*</url-pattern>
</servlet-mapping>
</web-app>
entry_page_gen.java
package Servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author oras
*/
@WebServlet(name = "entry_page_gen", urlPatterns = {"/entry/*"})
public class entry_page_gen extends HttpServlet {
/**
* 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 {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet entry_page_gen</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet entry_page_gen at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
entry_page_gen.java is stored in a package called Servlets.
What's going wrong? If more information is needed, please ask. I didn't want to bog down the question with info that might not be needed.
A point to note is that if I change the Web.xml -> <servlet-mapping> -> <url-pattern>
to /entry_page_gen
and call the servlet from a <form>
with the action
parameter, everything works fine.
P.S: I referred to this question while creating the above pieces of code.