创建servlet小程序:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>Insert title here servlet
Get方式请求HelloServlet
创建类,继承HTTPServlet
重写doGet,doPost方法
package com.servlet;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;//继承与HttpServletpublic class Servlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("处理Get()请求。。。。。"); PrintWriter out=response.getWriter(); response.setContentType("text/html;charset=utf-8");//设置字符集,否则会输出等标签代码 out.println(" Hello Servlet "); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("处理Post()请求。。。。。"); PrintWriter out=response.getWriter(); response.setContentType("text/html;charset=utf-8");//设置字符集,否则会输出 等标签代码 out.println(" Hello Servlet "); } }
在web.xml中注册
HelloServlet com.servlet.Servlet HelloServlet /com.servlet/Servlet