Tomcat中开启默认Servlet

 

1.将tomcat/conf/web.xml中以下代码的注释去掉,可以修改Servlet名、Url:/servlet/*

    
        invoker
        
          org.apache.catalina.servlets.InvokerServlet
        
        
            debug
            0
        
        2
    


    
        invoker
        /servlet/*
    

 

2.编写一个Servlet测试

package com.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NewServlet extends HttpServlet {

	private static final long serialVersionUID = -5027666740414457471L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.println("");
		out.println("显示默认Servlet信息");
		out.println("

显示默认Servlet信息

"); out.println(""); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

 

3.配置好启动目录启动Tomcat,如果在启动中遇到如下错误,则在tomcat/conf/context.xml中的标签中添加属性

严重: Error deploying web application directory test
java.lang.SecurityException: Servlet of class org.apache.catalina.servlets.Invok
erServlet is privileged and cannot be loaded by this web application

 

4.启动好后输入URL测试

http://localhost:8088/test/servlet/com.test.NewServlet

注意:com.test.NewServlet

 

5.如果不开户此功能会报404错误

 

 

 

 

 

 

你可能感兴趣的:(Servlet,Tomcat)