Servlet认识,servlet小程序,doGet,doPost
Posted 尚文韬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet认识,servlet小程序,doGet,doPost相关的知识,希望对你有一定的参考价值。
创建servlet小程序:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>Insert title here</title> </head> <body> <h1>servlet</h1> <hr> <a href="com.servlet/Servlet">Get方式请求HelloServlet</a> <form action="com.servlet/Servlet" method="post"> <input type="submit" value="Post方式请求HelloServlet"> </form> </body> </html>
创建类,继承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; //继承与HttpServlet public 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");//设置字符集,否则会输出<string>等标签代码 out.println("<strong>Hello Servlet</strong><br>"); } @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");//设置字符集,否则会输出<string>等标签代码 out.println("<strong>Hello Servlet</strong><br>"); } }
在web.xml中注册
<servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.servlet.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/com.servlet/Servlet</url-pattern> </servlet-mapping>
以上是关于Servlet认识,servlet小程序,doGet,doPost的主要内容,如果未能解决你的问题,请参考以下文章