动手学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 概述 首先,看 ...
随机推荐
- 模型(modle)
MVC模式三:模型操作数据库,之后注册变量于模板,之后用控制器的dispaly()方法显示; 一.跨控制器调用方法 因为控制器的实质是类,所以在该方法中造一个要调用的类的对象,调用他的方法; 比如,在 ...
- VBA実績表
VBA 插入一行保留样式 VBA 打开一个string指定的文件 VBA 按照文件类型名称打开一个文件 VBA excel中表示列的字母换成数字
- SQL SERVER 2008安装时出现不能在控件上调用 Invoke 或 BeginInvoke错误 解决方法
或者 SQL SERVER 2008安装时要求重启,但重启后仍要求重启.都可以使用此方法. 注册表的 "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet ...
- SPOJ #453. Sums in a Triangle (tutorial)
It is a small fun problem to solve. Since only a max sum is required (no need to print path), we can ...
- 剑指offer系列20--从上到下打印二叉树
* 20 [题目]从上往下打印出二叉树的每个节点,同层节点从左至右打印. * [思路]从根结点开始,先保存结点,再看根结点的左右结点有没有值. * 有,就将左右值放到集合中: * 根节点输出后,打印根 ...
- Axure RP
Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写. Axure RP是美国Axu ...
- 【freemaker】之Unknown built-in variable: now
Caused by: freemarker.core.ParseException: Unknown built-in variable: now 意思是没有内置变量,看了一下源码2.3.15版本fr ...
- item2,实现singleton模式
单例模式? 只能实现一个实例的类成为单例. ============== muduo库中单例模式实现 #include<boost/noncopyable.hpp> //#include ...
- ASP.NET ViewState详解
ASP.NET ViewState详解[转载] 作者:Infinities Loop 概述 ViewState是一个被误解很深的动物了.我希望通过此文章来澄清人们对ViewState的一些错误认识.为 ...
- 黄聪:wordpress如何获取当前页面的URL
一行代码搞定 <? echo home_url( add_query_arg( array() ) ); ?>