web组件 Servlet+练习

Servlet是Java技术对CGI的回答。Servlet程序在服务器端运行,动态的生成web页面。

1.效率高  (轻量级的Java线程)

2.容易使用 (自动解析和解码HTML表单数据,读取和设置HTTP头,)

3.功能强大 (直接和web服务器交互)

4.具有更好的可移植性,节省投资

生命周期的各个阶段:

(1)加载和实例化

(2)初始化 initf()方法

(3)请求处理  doGet()  doPost() doDelete() doPut()

(4)服务终止  destroy()

HttpServlet介绍

  1. doGet()     用来处理HTTP的GET请求,通过GenericServlet类的service()方法来调用
  2. doPost()   通过Internet发送大量数据     request请求    response响应
  3. HttpServletRequest类的request对象是存储客户端请求的
  4. HttpServletResponse类的response对象是包含服务器对客户端做出响应的对象
  5. doPut()  模拟通过FTP发送一个文件

HttpServlet容器相应Web客户请求流程

Web客户向Servlet容器发送HTTP请求,Servlet容器解析Web客户的Http请求,Servlet容创建一个HttpRequest对象,在这个对象中封装HTTP请求信息,Servlet容器创建一个HttpResponse对象,调用service()方法把俩个对象作为参数传给HttpServlet对象,调用HttpRequest有关方法,获取http请求信息,调用HttpResponse的有关方法生成响应数据,最后把HttpServlet的响应结果传递给Web客户。

 

练习:创建web工程和html网页连接(and注意事项)

  1. new一个web project   建议Java EE version设置成web2.5(可能是我电脑的问题,你们可以设置成3.0试试)
  2. Web里的Module里的Geberate web.xml一定要点,要不然没有.xml文件
  3. 第一步在src离创建个servlet
  4. WebRoot文件里的web.xml会自动生成 双击web.xml文件,右下角有个source点击进入编辑页面
  5. <?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>qwe</display-name>
      <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>
        <description></description>
        <display-name>Abc</display-name>
        <servlet-name>Abc</servlet-name>
        <servlet-class>com.asasd.Abc</servlet-class>
    ***************************************************
      </servlet>
      <servlet-mapping>
        <servlet-name>Abc</servlet-name>
        <url-pattern>/Abc</url-pattern>
      </servlet-mapping>
    </web-app>

    正常应该是这样的

  6. 在我加*****号位置加入

    <init-param>
          <param-name>name</param-name>
          <param-value>xxx</param-value>
        </init-param>
        <init-param>
          <param-name>password</param-name>
          <param-value>123456</param-value>
        </init-param>

    name和password是自己设置的

  7. 返回.java文件在doGet方法里加入

    		ServletConfig servletConfig = this.getServletConfig();
    		String name1 = servletConfig.getInitParameter("name");
    		String password1 = servletConfig.getInitParameter("password");
            response.setContentType("text/html;charser=utf-8");
    		PrintWriter out = response.getWriter();
    		out.print("<!DOCTYPE html>");
    		out.print("<html>");
    		out.print("<head><title>wo</title></head>");
    		out.print("<body>");
    		out.print(name1+"<br>");
    		out.print(pass1+"<br>");
    		out.print("</body>");
    		out.print("</html>");
    		
    		}

    点击运行弹出一个窗口里面有用户名和密码就可以了

  8. 注意:弄完以后要关闭服务器(点击状态栏里console里的红色按钮)

  9. 第二步:把之前弄的注册界面复制过来,可以自己创建一个.jps文件,把代码在复制过去

  10. 在.java文件里在把之前写的都注释了,然后加入

  11. request.setCharacterEncoding("utf-8");  //解决乱码
    		response.setContentType("text/html; charset=gb2312"); //解决乱码
    		String username = request.getParameter("nnaammee");
    		String password = request.getParameter("ppaass");
    		String sexx = request.getParameter("sex");
    		String habbString[] = request.getParameterValues("wan");
    		String shi = request.getParameter("dizhi");
    		String qu = request.getParameter("shi");
    		String xString = request.getParameter("qu");
    		String text = request.getParameter("teexxtt");
    		String aihaoString = "";
    		if(habbString!=null)
    		for(int i=0;i<habbString.length;i++)
    			aihaoString += habbString[i]+",";
    		response.getWriter().write("用户名: " + username);
    		response.getWriter().write("密码: " + password);	
    		response.getWriter().write("性别: " + sexx);	
    		response.getWriter().write("爱好: " + aihaoString);	
    		response.getWriter().write("地址: " + shi+qu+xString);	
    		response.getWriter().write("其他: " + text);	

    里面的各种名字都需要和.jsp里面的名字相匹配

  12. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    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 'MyJsp.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>
      <form action="Hello" method="get">
                <table border="1" cellspacing="" cellpadding="">
                    <tr><td align="center" colspan="2">用户注册页面</td></tr>
                    <tr><td>用户名:</td><td><input type="text" name="nnaammee" size="29"></td></tr>
                    <tr><td>密码:</td><td><input type="password" name="ppaass" size="30"></td></tr>
                    <tr><td align="center" colspan="2"><input type="button" value="确定" size="10"></td></tr>
                    <tr><td>性别:</td><td><input type="radio" name="sex" value="nan">男 <input type="radio" name="sex" value="nv">女</td></tr>
                    <tr><td>爱好:</td><td><input type="checkbox" name="wan" value="玩">玩游戏 
                    <input type="checkbox" name="wan" value="吃">吃东西 
                    <input type="checkbox" name="wan" value="睡">睡觉</td></tr>
                    <tr><td>家庭住址:</td><td><select name="dizhi">
                    <option value="黑龙江">黑龙江</option>
                    <option value="北京">北京</option>
                    <option value="台湾">台湾</option>
                    </select>
                    <select name="shi">
                    <option value="台北">台北</option>
                    <option value="哈尔滨">哈尔滨</option>
                    <option value="天安门">天安门</option>
                    </select>
                    <select name="qu">
                    <option value="台北都会区">台北都会区</option>
                    <option value="宾西镇">宾西镇</option>
                    <option value="故宫">故宫</option>
                    </select>
                    </td></tr>
                    <tr><td colspan="2">上传文件:<input type="file"></td></tr>
                    <tr><td align="center">其他:</td><td><textarea name="teexxtt" rows="10" cols="30">
                    </textarea></td></tr>
                    <tr><td colspan="2" align="center"><input type="submit" value="提交"></td></tr>
                    
                </table>
                </form>
      </body>
    </html>

    然后再在.java文件处运行就可以了

  13. 会弹出一个现有null的页面

  14. 打开浏览器 输入http://localhost:8080/web项目名/servlet名.jsp 弹出页面后输入点击提交 就ok了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值