javaweb学习(4)--servlet相关

一、servlet的生命周期
我们写一个程序验证下,直接上代码:

public class ServletLife extends HttpServlet{

    public ServletLife(){
        System.out.println("construction");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("init");
    }

    @Override
    public void destroy() {
        System.out.println("destory");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet");
    }
}

里面包含了下面几个方法:

construction
init 
doGet(),doPost()… 
destroy

发现在第一次访问网址的时候,调用的顺序为

construction
init 
doGet

后面继续调用的话,就只会调用doGet()。因为在一个服务端只会调用创建一个一样的servlet。
然后destory是在shutdown的时候调用。

所以,可以得出servlet的生命周期为:

加载 classLoader
    构造 construction
初始化 init 
调用server()doGet(),doPost()…
    服务会一直运行
销毁 destroy

2。获取客户端上传的值
直接上代码:

public class ParmServlet extends HttpServlet{
    protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException ,java.io.IOException {
        PrintWriter writer = resp.getWriter();
        resp.setContentType("text/html");
        writer.println(req.getParameter("parm1"));
        writer.println("
"
); }; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); resp.setContentType("text/html"); writer.println(req.getParameter("parm1")); writer.println("doPost"); writer.println("
"
); } }

html的代码为:



<html xmlns="http://www.w3.org/1999/xhtml">
<title><% m_webPage & departName; %>title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
head>
<body>
<div align="center" >
<form name="frm" id="frm" method="post" action="ParmServlet">
    <table>
        <tr>
            <td>

                <input name="parm1" id="parm1" type="text" >input>
            td>           
        tr>
        <tr>
            <td>
                <input name="parm2" id="parm2" type="text" >input>
            td>
        tr>
    table>
    <button type="sumbit">提交button>
body>
html>

method=”post” 这个如果是get就会调用doGet方法。
其实这个主要的点就是:req.getParameter(“parm1”);

3。获取客户端上传的多个值


public class GetAllParmsServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();
        //容器,所有的
        Enumeration parmNames = req.getParameterNames();
        while(parmNames.hasMoreElements()){
            String parmName =  parmNames.nextElement();
            String[] parameter = req.getParameterValues(parmName);
            if(parameter.length ==1){
                writer.println(parmName + ":" + parameter[0]);
            }else{
                for(int i=0;i":" + parameter[i]);
                }
            }
            writer.println("
"
); } writer.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

html文件:



<html xmlns="http://www.w3.org/1999/xhtml">
<title><% m_webPage & departName; %>title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
head>
<body>
<div align="center" >
<form name="frm" id="frm" method="get" action="GetAllParmsServlet">
    <table>
        <tr>
            <td>
                个数:<input name="parm0" id="parm0" type="text" >input>
            td>           
        tr>
        <tr>
            <td>
                产品名字:<input name="parm1" id="parm1" type="text" >input>
            td>           
        tr>
        <tr>
            <td>
                产品名字2:<input name="parm1" id="parm1" type="text" >input>
            td>           
        tr>
        <tr>
            <td>
                未定义:<input name="parm2" id="parm2" type="text" >input>
            td>
        tr>
        <tr>
            <td>
                未定义2:<input name="parm3" id="parm3" type="text" >input>
            td>
        tr>
    table>
    <button type="sumbit">提交button>
body>
html>

这里的重点其实就是
Enumeration parmNames = req.getParameterNames();
然后使用这个容器进行查询

你可能感兴趣的:(web)