动手学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 概述 首先,看 ...
随机推荐
- Java中的定时器Timer
java.util.Timer是一个实用工具类,该类用来调度一个线程,使线程可以在将来某一时刻开始执行. Java的Timer类可以调度一个线程运行一次,或定期运行. java.util.TimerT ...
- php 同步因子的并发处理
在php中,如果处理支付时,会涉及到并发. 具体体现在同步通知支付结果和异步通知结果. 拿支付宝来说,同步通知call_back和异步通知notify是没有固定先后顺序的. 有可能notify先通知到 ...
- (转)LitJson 遍历key
本文转载自:http://blog.csdn.net/inlet511/article/details/47127579 用LitJson插件获取到的对象,如果想遍历对象中包含的子对象的key,可以用 ...
- (转)WEB第三方打印控件[ASP.NET常用工具]
本文转载自:http://blog.csdn.net/chz_cslg/article/details/25415347 在B/S模式开发中,打印是个很大的困扰.无论是采用页面直接输出或者引用WORD ...
- Redis-sentinel监控
Sentinel介绍 Redis的 Sentinel 系统用于管理多个Redis服务器, 该系统执行以下三个任务: 监控(Monitoring) 提醒(Notification) 自动故障迁移(Aut ...
- poj2352消防站
题目大意:有n个点的一棵树,每个点有两个值:w和c.现在要在其中若干点建立消防站,使得每个点到最近的消防站的距离不超过该点的c值,i点建立消防站的费用为w.求最小费用. 分析:本题显然是树型Dp.定义 ...
- PHP之单例模式的实现
单例模式: 单例模式又称职责模式:简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 单例类: 1.构造函数需要标记为private(访问控制:防止外部代码使用n ...
- Hadoop学习1--解决启动过程中的问题
方法:http://www.aboutyun.com/thread-12694-1-1.html http://www.linuxidc.com/topicnews.aspx?tid=13 http: ...
- EntityFramework ,ef 介绍
EntityFramework之领域驱动设计实践 分层架构 在引入实例以前,我们有必要回顾,并进一步了解分层架构.“层”是一种体系结构模式[POSA1],也是被广大软件从业人员用得最为广泛而且最为灵活 ...
- 在Visual Studio 2010/2012中 找不到创建WebService的项目模板
参考文章: http://blog.sina.com.cn/s/blog_6d545999010152wb.html 在 Visual Studio 2010 或者2012的新建 Web 应用程序或者 ...