动手学servlet(二) servlet基础
1、我们来试着向一个servlet提交一个表单,现在webcontent下新建一个login.html页面,其中action对应servelt类名,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>提交表单</title>
</head>
<body>
<form action="LoginServlet" method="post">
用户名:<input name="username" type="text"><br>
密码:<input name="password" type="password"><br>
<input value="提交" name="submit" type="submit">
</form>
</body>
</html>
代码
附:request.getParameter("username");可以读取用户名输入框的值,针对复选框需要调用request.getParameterValues(name);将返回一个字符串数组
2、新建一个servlet类LoginServlet,代码如下:
package servletdemo; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
System.out.print("loginservlet...");
// TODO Auto-generated constructor stub
} @Override
public void init() throws ServletException {
// TODO 自动生成的方法存根
System.out.print("init...");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.print("doget...");
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.print("dopost...");
} @Override
public void destroy() {
// TODO 自动生成的方法存根
System.out.print("destory...");
} }
代码
注意:右键新建一个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的内容,具体看例子
//需要跳转的servlet的doGet()方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
out.print("<h1>LoginServlet</h1>");
request.getRequestDispatcher("MyServlet").include(request, response);
}
//跳转到的MyServlet的doGet()方法
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO 自动生成的方法存根
String result=this.getServletContext().getInitParameter("henry");
System.out.print(result);
resp.setContentType("text/html;charset=gbk");
PrintWriter pw=resp.getWriter();
pw.write(result);
pw.close();
}
代码

- 请求跳转
也是跳转到另外一个servlet,但是不输出本servlet的内容
//需要跳转的servlet的doGet()方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
out.print("<h1>LoginServlet</h1>");
request.getRequestDispatcher("MyServlet").forward(request, response);
}
//跳转到的MyServlet的doGet()方法
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO 自动生成的方法存根
String result=this.getServletContext().getInitParameter("henry");
System.out.print(result);
resp.setContentType("text/html;charset=gbk");
PrintWriter pw=resp.getWriter();
pw.write(result);
pw.close();
}
代码

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

动手学servlet(二) servlet基础的更多相关文章
- Servlet一(web基础学习笔记二十)
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- 动手学servlet(六) 过滤器和监听器
过滤器(Filter) 过滤器是在客户端和请求资源之间,起一个过滤的作用,举个例子,比如我们要请求admin文件夹下的index.jsp这个页面,那么我们可以用一个过滤器,判断登录用户是不是管理员 ...
- web开发之Servlet 二
在上一篇文章中,我们演示也证明了Servlet 是一种动态web资源开发的技术,即我可以在浏览器中输入URL,然后就可以在浏览器中看到我们编写的Servlet资源. 那当我们在浏览器上一起一个HTTP ...
- Tomcat深入浅出——Servlet(二)
一.Servlet简介 Servlet类最终开发步骤: 第一步:编写一个Servlet类,直接继承HttpServlet 第二步:重写doGet方法或者doPost方法,重写哪个我说的算! 第三步:将 ...
- Java Servlet(二):servlet配置及生命周期相关(jdk7+tomcat7+eclipse)
该篇文章记录了Servlet配置相关用法及Servlet在Servlet容器中生命周期方法. Tomcat是一个Servlet容器: 1.Servlet容器管理了Servlet的整个生命周期,并调用s ...
- servlet之servlet(二)
·servlet用于创建返回基于客服请求的动态页面(整个).部分页面.与数据库交互 ·servlet接口: 继承servlet接口后,要在web.xml中配置和映射servlet.配置servlet初 ...
- Servlet技术——Servlet基础
Servlet是运行在Web服务器端的Java应用程序,它使用Java语言编写,具有Java语言的优点.与Java程序的区别是,Servlet对象主要封装了对HTTP请求的处理,并且它的运行需要Ser ...
- 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 ...
- Mina 系列(二)之基础
Mina 系列(二)之基础 Mina 使用起来多么简洁方便呀,就是不具备 Java NIO 的基础,只要了解 Mina 常用的 API,就可以灵活使用并完成应用开发. 1. Mina 概述 首先,看 ...
随机推荐
- PHP header函数大全
PHP header函数大全 header('Content-Type: text/html; charset=utf-8'); header('Location: http://www.php-no ...
- Python 通过pickle标准库加载和保存数据对象
import pickle with open('mydata.pickle','wb') as mysavedata: pickle.dump([1,2,'three'], mysavedata) ...
- [linux basic 基础]----同步信号量
直接使用一个共享变量,来是两个线程之间进行切换是非常笨拙而且没有效率的:信号量--互斥量--这两者是相互通过对方来实现的:比如,如果想控制某一时刻只有一个线程可以访问一些共享内存,使用互斥量要自然一些 ...
- 单源最短路径——Floyd算法
正如我们所知道的,Floyd算法用于求最短路径.Floyd算法可以说是Warshall算法的扩展,三个for循环就可以解决问题,所以它的时间复杂度为O(n^3). Floyd算法的基本思想如下:从任意 ...
- Patial修饰符
C#中新建一个Windows Form时,后台代码都会自动添加如下代码: public partial class Form1 : Form { public Form1() { Initialize ...
- 探秘JavaScript中的六个字符
JavaScript 是一个奇怪而有趣的语言,我们可以写一些疯狂却仍然有效的代码.它试图帮助我们把事情转换到基于我们如何对待他们的特定类型. 如果我们添加一个字符串,JavaScript会假定我们希望 ...
- QRCode.jar生成二维码
参考http://www.oschina.net/code/snippet_2252392_45457 package com.ORcode; import java.awt.image.Buffer ...
- cf666 C. Codeword 组合数学 离线分块思想
time limit per test 6 seconds memory limit per test 256 megabytes input standard i ...
- centos6.5安装mysql记录
1.查看操作系统相关信息. [root@linuxidc ~]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m [root@ ...
- Maven详解之仓库------本地仓库、远程仓库
在Maven中,任何一个依赖.插件或者项目构建的输出,都可以称之为构件. Maven在某个统一的位置存储所有项目的共享的构件,这个统一的位置,我们就称之为仓库.(仓库就是存放依赖和插件的地方) 任何的 ...