action="post" 、 servletconfig 、 servletcontext 、getPrintWiter() 、context-param、 init-param(第一个完整的servlet)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>注册</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head> 
  <body>
   <form method="post" action="/ServletOne/registered.do">
        <table width="400px" height="300px">
          <tr>
            <td align="right">用户名:</td>
            <td><input type="text" name="username"/></td>
          <tr>
          <tr>
            <td align="right">密码:</td>
            <td><input type="password" name="passwd"/></td>
          <tr>
          
          <tr>
                <td colspan="2" align="center"><input type="submit" value="注册"/></td>
          </tr>
        </table>
   </form>
   
  </body>
</html>
package registered;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RegisteredServlet extends HttpServlet {

    public void init() throws ServletException {
        ServletConfig scf=this.getServletConfig();
        ServletContext sct=this.getServletContext();
        //获取init配置参数指定参数     config   jack
        System.out.println("config   "+scf.getInitParameter("name"));
        //获取整个init配置参数数组    Enumeration  name   Enumeration  age
        Enumeration e=scf.getInitParameterNames();
        while(e.hasMoreElements()){
            System.out.println("Enumeration  "+e.nextElement().toString());
        }
        //获取context-param参数值      context  ServletContextValue
        System.out.println("context  "+sct.getInitParameter("ServletContextName"));
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("DoGet()");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("DoPost()");
        String name=request.getParameter("username");
        String passwd=request.getParameter("passwd");
        PrintWriter out=response.getWriter();
        out.write("username="+name);
        out.write("<br/>");
        out.write("passwd="+passwd);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>ServletOne</display-name>
    <context-param>
        <param-name>ServletContextName</param-name>
        <param-value>ServletContextValue</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <!--配置时一律小写 -->
    <servlet>
        <servlet-name>Servlet01</servlet-name>
        <servlet-class>servlet_1121.Servlet01</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet01</servlet-name>
        <url-pattern>/s1</url-pattern>
    </servlet-mapping>
    <!-- 注册 -->
    <servlet>
        <servlet-name>RegisteredServlet</servlet-name>
        <servlet-class>registered.RegisteredServlet</servlet-class>
        <init-param>
           <param-name>name</param-name>
           <param-value>jack</param-value>
        </init-param>
        <init-param>
           <param-name>age</param-name>
           <param-value>20</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>RegisteredServlet</servlet-name>
        <url-pattern>/registered.do</url-pattern>
    </servlet-mapping>
</web-app>

 

posted @ 2016-11-22 13:19  奋斗的少年WH  阅读(165)  评论(0编辑  收藏  举报