第一个HttpServlet 程序

HttpServletDemo01.java

package com.fl.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpServletDemo01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("get+++++++++++");
        resp.getOutputStream().write("doGet+++被调用了".getBytes());
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("post+++++++++++++");
        resp.getOutputStream().write("dopost+++被调用了".getBytes());    
    }
}

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
    <servlet>
     <!-- 类名 -->
    <servlet-name>httpGet</servlet-name><!-- 这里实现类名和实际运行的程序的连接 -->
    <!-- 所在的包 -->
    <servlet-class>com.fl.servlet.HttpServletDemo01</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>httpGet</servlet-name><!-- 类名和浏览器输入的网址的连接 -->
    <!-- 访问的网址 -->
    <url-pattern>/httpGt</url-pattern>
    </servlet-mapping>
</web-app>

 

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
  <body>
  <form action="httpGt" method="post">
          <input type="submit" value="submit">
          
  </form>
  </body>
</html>

也就是增加了这部分:

  <body>
    <form action="httpGt" method="post">      -----------------这里的httpGt与xml中的类名一致,会请求post。
            <input type="submit" value="submit">
    </form>
  </body>


部署进tomcat后,
使用浏览器输入:http://localhost:8080/first_servlet/index.jsp



如何设置,使得在服务器启动时,就运行init:
只需要修改下该程序所对应的xml文件即可。

<servlet>
<!-- 类名 -->
<servlet-name>HelloWorld</servlet-name><!-- 这里实现类名和实际运行的程序的连接 -->
<!-- 所在的包 -->
<servlet-class>com.fl.servlet.ServletDemo01</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

 

这个数字必须大于0。在大于0的基础上,各个服务之间,数字越小的越先初始化。




posted @ 2019-04-07 22:18  奇婆子  阅读(354)  评论(0编辑  收藏  举报