1、我们来试着向一个servlet提交一个表单,现在webcontent下新建一个login.html页面,其中action对应servelt类名,代码如下:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  5. <title>提交表单</title>
  6. </head>
  7. <body>
  8. <form action="LoginServlet" method="post">
  9. 用户名:<input name="username" type="text"><br>
  10. 密码:<input name="password" type="password"><br>
  11. <input value="提交" name="submit" type="submit">
  12. </form>
  13. </body>
  14. </html>

代码

附:request.getParameter("username");可以读取用户名输入框的值,针对复选框需要调用request.getParameterValues(name);将返回一个字符串数组

2、新建一个servlet类LoginServlet,代码如下:

  1. package servletdemo;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. /**
  11. * Servlet implementation class LoginServlet
  12. */
  13. @WebServlet("/LoginServlet")
  14. public class LoginServlet extends HttpServlet {
  15. private static final long serialVersionUID = 1L;
  16.  
  17. /**
  18. * @see HttpServlet#HttpServlet()
  19. */
  20. public LoginServlet() {
  21. System.out.print("loginservlet...");
  22. // TODO Auto-generated constructor stub
  23. }
  24.  
  25. @Override
  26. public void init() throws ServletException {
  27. // TODO 自动生成的方法存根
  28. System.out.print("init...");
  29. }
  30. /**
  31. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  32. */
  33. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  34. // TODO Auto-generated method stub
  35. System.out.print("doget...");
  36. }
  37.  
  38. /**
  39. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  40. */
  41. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  42. // TODO Auto-generated method stub
  43. System.out.print("dopost...");
  44. }
  45.  
  46. @Override
  47. public void destroy() {
  48. // TODO 自动生成的方法存根
  49. System.out.print("destory...");
  50. }
  51.  
  52. }

代码

注意:右键新建一个servlet不会在web.xml生成对应的配置标签,是因为servlet类里已经有了@WebServlet注解

3、运行login.html,可以验证servlet的生命周期,效果如下:

4、有时候需要获得web.xml里一些配置信息,例如下面的配置,其中name相当于key,value就是对应的值

<context-param>
<param-name>henry</param-name>
<param-value>123</param-value>
</context-param>

用这行代码就可以获得KEY对应的值:String result=this.getServletContext().getInitParameter("henry");  

5、servlet在MVC中相当于controller,所以经常需要做一些页面的跳转,下面来看看页面导航的实现

  • 请求重定向

直接跳转到其他的网站,response.sendRedirect("http://www.baidu.com");

  • 请求包含

就是跳转到另外一个serlvet,但是也输出本servlet的内容,具体看例子

  1. //需要跳转的servlet的doGet()方法
  2.  
  3. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  4. // TODO Auto-generated method stub
  5. PrintWriter out=response.getWriter();
  6. out.print("<h1>LoginServlet</h1>");
  7. request.getRequestDispatcher("MyServlet").include(request, response);
  8. }
  9.  
  10. //跳转到的MyServlet的doGet()方法
  11. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  12. throws ServletException, IOException {
  13. // TODO 自动生成的方法存根
  14. String result=this.getServletContext().getInitParameter("henry");
  15. System.out.print(result);
  16. resp.setContentType("text/html;charset=gbk");
  17. PrintWriter pw=resp.getWriter();
  18. pw.write(result);
  19. pw.close();
  20. }

代码

  • 请求跳转

也是跳转到另外一个servlet,但是不输出本servlet的内容

  1. //需要跳转的servlet的doGet()方法
  2.  
  3. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  4. // TODO Auto-generated method stub
  5. PrintWriter out=response.getWriter();
  6. out.print("<h1>LoginServlet</h1>");
  7. request.getRequestDispatcher("MyServlet").forward(request, response);
  8. }
  9.  
  10. //跳转到的MyServlet的doGet()方法
  11. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  12. throws ServletException, IOException {
  13. // TODO 自动生成的方法存根
  14. String result=this.getServletContext().getInitParameter("henry");
  15. System.out.print(result);
  16. resp.setContentType("text/html;charset=gbk");
  17. PrintWriter pw=resp.getWriter();
  18. pw.write(result);
  19. pw.close();
  20. }

代码

 注意:getRequestDispatcher()方法的参数不是servlet类名,而是web.xml里url-pattern节点中的值

动手学servlet(二) servlet基础的更多相关文章

  1. Servlet一(web基础学习笔记二十)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  2. 动手学servlet(六) 过滤器和监听器

     过滤器(Filter) 过滤器是在客户端和请求资源之间,起一个过滤的作用,举个例子,比如我们要请求admin文件夹下的index.jsp这个页面,那么我们可以用一个过滤器,判断登录用户是不是管理员 ...

  3. web开发之Servlet 二

    在上一篇文章中,我们演示也证明了Servlet 是一种动态web资源开发的技术,即我可以在浏览器中输入URL,然后就可以在浏览器中看到我们编写的Servlet资源. 那当我们在浏览器上一起一个HTTP ...

  4. Tomcat深入浅出——Servlet(二)

    一.Servlet简介 Servlet类最终开发步骤: 第一步:编写一个Servlet类,直接继承HttpServlet 第二步:重写doGet方法或者doPost方法,重写哪个我说的算! 第三步:将 ...

  5. Java Servlet(二):servlet配置及生命周期相关(jdk7+tomcat7+eclipse)

    该篇文章记录了Servlet配置相关用法及Servlet在Servlet容器中生命周期方法. Tomcat是一个Servlet容器: 1.Servlet容器管理了Servlet的整个生命周期,并调用s ...

  6. servlet之servlet(二)

    ·servlet用于创建返回基于客服请求的动态页面(整个).部分页面.与数据库交互 ·servlet接口: 继承servlet接口后,要在web.xml中配置和映射servlet.配置servlet初 ...

  7. Servlet技术——Servlet基础

    Servlet是运行在Web服务器端的Java应用程序,它使用Java语言编写,具有Java语言的优点.与Java程序的区别是,Servlet对象主要封装了对HTTP请求的处理,并且它的运行需要Ser ...

  8. Rhythmk 一步一步学 JAVA (17):Servlet 文件上传

    1.环境 : JDK 1.6 , Tomcat 7.0 2.第三方类库: commons-fileupload-1.3.1.jar commons-io-2.4.jar 3.web.xml配置: &l ...

  9. Mina 系列(二)之基础

    Mina 系列(二)之基础 Mina 使用起来多么简洁方便呀,就是不具备 Java NIO 的基础,只要了解 Mina 常用的 API,就可以灵活使用并完成应用开发. 1. Mina 概述 首先,看 ...

随机推荐

  1. 数据库连接工具类——包含取得连接和关闭资源 ConnUtil.java

    package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...

  2. openssl与cryptoAPI交互AES加密解密

    继上次只有CryptoAPI的加密后,这次要实现openssl的了 动机:利用CryptoAPI制作windows的IE,火狐和chrome加密控件后,这次得加上与android的加密信息交互 先前有 ...

  3. ettercap ARP dns 欺骗

    1.arp 这个简单,太熟了.略过1     2.dns   根据arp欺骗的步骤. 多了个etter.dns文件 找到它:locate etter.dns 进入后添加dns正向解析     启动,选 ...

  4. css3之transform的应用

    一.利用transform实现图片额外显示 效果图如下 初始状态:

  5. Linux命令详解nice

    [命令]nice — 调整程序运行的优先级 [格式]nice [OPTION] [command [arguments...]] [说明] 在当前程序运行优先级基础之上调整指定值得到新的程序运行优先级 ...

  6. phpize的安装

    一直想装VLD却一直没装上,因为需要用到phpize,但这个工具大部分机子都没有装,上网搜了一下大部分都是讲phpize的应用没有讲怎么安装. 今天终于搜到了,不过是要在linux机器上,有yum命令 ...

  7. oracle imp导入数据到另一个表空间

    http://blog.163.com/darlingchenlin@126/blog/static/7156283420100531431855/ 1.在第一个数据库导出数据:qlyg_xs_db_ ...

  8. PYTHON对文件及文件夹的一些操作

    python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件:1) os.mknod("test.txt") 创建空文件2) open("test. ...

  9. SpringMVC控制器设值原理分析(ModelAndView的值通过HttpServletRequest直接取到的原因)

    @RequestMapping("/userlist.do") public String getUserList(Model model){ HttpServletRequest ...

  10. [JS]鼠标事件穿透的问题

    今天制作登陆窗口的效果时碰到的一个问题,如下: 标签结构如下: <div id="loginFrame"> <form class="loginFram ...