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>
  • 相关阅读:
    sci会议和sci期刊区别是什么
    微信小程序保存图片到相册
    详解python3如何调用c语言代码
    微信小程序的跳转navigateTo()和redirectTo()用法和区别
    python+opencv图像变换的两种方法cv2.warpAffine和cv2.warpPerspective
    微信小程序
    微信小程序不同机型的兼容布局解决
    cv2.warpAffine 参数详解
    np.vstack()和np.hstack()
    numpy.linalg.svd函数
  • 原文地址:https://www.cnblogs.com/1ming/p/9553090.html
Copyright © 2011-2022 走看看