编写servlet的404历程

文章目录

  • 1、首先是项目结构
  • 2、代码
    • (1)HelloWorld
    • (2)web.xml
  • 3、404解决
    • (1)查看当前servlet包名类名引用是否正确
    • (2)路径因是否正确
    • (3)查看tomcat是否部署在eclipse中
  • 4、最后感谢党

1、首先是项目结构

编写servlet的404历程_第1张图片

2、代码

(1)HelloWorld

package com.robin;

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;

/**
 * Servlet implementation class HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String msg;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
        msg = "nm:404";
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.getWriter().append("Served at: ").append(request.getContextPath());
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.println("

"+msg+"

"
); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

(2)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>servlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>com.robin.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/servlet1/HelloWorld</url-pattern>
  </servlet-mapping>
</web-app>

3、404解决

(1)查看当前servlet包名类名引用是否正确

编写servlet的404历程_第2张图片

(2)路径因是否正确

格式为/servlet1/HelloWorld,如果只是/HelloWorld,我这边会报错
编写servlet的404历程_第3张图片

(3)查看tomcat是否部署在eclipse中

http://localhost:8080/ 是否能够正常访问

tomcat属性
编写servlet的404历程_第4张图片
正常访问页面:
编写servlet的404历程_第5张图片

4、最后感谢党

编写servlet的404历程_第6张图片

你可能感兴趣的:(spring)