1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ORAS
  • 39
  • 3

2 Answers2

0

Well here is a solution for you.

entry_page_gen.java (By the way you should change this name to follow the java convention)

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //processRequest(request, response);
     response.sendRedirect("hello.html");
     return;
}

Configuration:

enter image description here

Output:

enter image description here

Notes:

In the image you are seeing the URL http://localhost:8080/web-project/hello.html because of the redirect in the method, but you can reach this element with: http://localhost:8080/web-project/entry

The explanation of the configuration could see here: https://stackoverflow.com/a/26329628/1670134

sirandy
  • 1,834
  • 5
  • 27
  • 32
0

As answered in here, the initial slash character refers to the path from the base URL (by default the domain name, unless changed with a <base> HTML element). So if your app URL is http://www.example.com/myapp, then an anchor <a href="/entry/test.html"> 1 </a> will refer to http://www.example.com/entry/test.html, therefore the servlet is not called.

You could change the anchor's href to this <a href="/myapp/entry/test.html"> 1 </a>

Gabriel Belingueres
  • 2,945
  • 1
  • 24
  • 32