WebServlet使用方法

在servlet3.0以后,我们可以不用再web.xml里面配置servlet,只需要加上@WebServlet注解就可以修改该servlet的属性了。
**

1.Form表单连WebServlet**

Servlet部分:

package WebTest1;

import java.io.IOException;
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 WebTest1
 */
//对类进行标记,注解。框架搭建者并不知道使用框架的人所建立的类,所以要通过反射来获取用户定义的类。
@WebServlet("/WebTest1")
public class WebTest1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WebTest1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	 //处理Get请求
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决中文乱码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		//接收参数
		String str = request.getParameter("mytext");
		//返回数据
		response.getWriter().print(str+"你好");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	 //处理Post请求
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Html5部分:





Insert title here



	
要发送的数据:

运行并输入:彭健
在这里插入图片描述
结果:
在这里插入图片描述
网页访问:localhost:端口号/文件夹名/html文件名.html
WebServlet使用方法_第1张图片
结果:
WebServlet使用方法_第2张图片

2.通过ajax连WebServlet

WebServlet部分:(同上面一样)

package WebTest1;

import java.io.IOException;
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 WebTest1
 */
//对类进行标记,注解。框架搭建者并不知道使用框架的人所建立的类,所以要通过反射来获取用户定义的类。
@WebServlet("/WebTest1")
public class WebTest1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public WebTest1() {
        super();
        // TODO Auto-generated constructor stub
    }

//处理Get请求
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决中文乱码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		//接收参数
		String str = request.getParameter("mytext");
		//返回数据
		response.getWriter().print(str+"你好");
	}
	//处理Post请求
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

HTML5部分:





Insert title here



	数据:
	




运行结果:
WebServlet使用方法_第3张图片

你可能感兴趣的:(WebServlet)