Why the servlet doesn't work?

All, I am the newbie into the Java web development, I am trying to implement a Servlet in my test. But I found the Servlet I created doesn't work . I didn't know whether I had missed anything. Please help me to review it .thanks.

What I had done so far is:

  1. Created a Dynamic Web Project named SecondWeb with the option Generate web.xml DD.
  2. Added a Servlet named HelloServlet under the package com.example.servlets. I config the URL Mapping with the values /HelloServlet and /*. Hope it will work for all the url pattern under the root.

Here is the code for it .

package com.example.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: HelloServlet
 *
 */
 public class HelloServlet extends javax.servlet.http.HttpServlet 
     implements javax.servlet.Servlet {

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
    }       

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, 
         HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        response.getWriter().write("Hello, world!");
    }   

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, 
         HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        // TODO Auto-generated method stub
    }               
}

And the I also added a index.jsp joined the test.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Title</title>
</head>
<body>
<% java.util.Date d = new java.util.Date(); %>
<h1>
Today's date is <%= d.toString() %> and this jsp page worked!
</h1>
</body>
</html>

What I expected is I hope the Hello world string can be added into the index.jsp response HTML when access the URL http://localhost:8080/SecondWeb or http://localhost:8080/SecondWeb/index.jsp. But seems the HelloServlet doesn't work. Why? Thanks.

Answers


I found the issue, I have to add the Servlet configuration in Web.xml. Like below under the root element:

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.example.servlets.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

Need Your Help

How to include the CSRF token in the headers in Dropzone upload request?

javascript laravel-5 csrf dropzone.js

I am working on a single page application and I am using Laravel 5 for the web service.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.