zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    hdu 3996
    poj 3189
    poj 2391
    zoj 3165
    【Visual Studio】
    httpwebrequest Winform 上传图片
    [MVC] win7 下 配置 IIS 问题
    win7 下 升级 vs2008
    [Visual Studio 2010] NET 4.0 WinForm无法引用System.Web.dll的解决方法
    [XML] XML
  • 原文地址:https://www.cnblogs.com/1ming/p/9553090.html
Copyright © 2011-2022 走看看