Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。

它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。

Servlet是位于Web服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同。

Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。

Servlet实例化过程

1. servlet容器负责创建servlet的一个实例(在第一次请求servlet的时候).

2. 容器调用该实例的init()方法完成初始化工作.

3. 如果容器对该servlet有请求,则调用此实例的service()方法,service()方法根据请求类型(get还是post)决定调用doXXX()方法.

4. 当web应用被终止时,容器在销毁本实例前调用它的destroy()方法.

5. 销毁并标记该实例以供作为垃圾收集.

在servlet生命周期中,servlet的初始化和和销毁阶段只会发生一次。

而service方法执行的次数则取决于servlet被客户端访问的次数。

Servlet如何处理请求

当用户发送一个请求到某个Servlet的时候,Servlet容器会创建一个ServletRequst和ServletResponse对象.

在ServletRequst对象中封装了用户的请求信息,然后Servlet容器把创建好的ServletRequst和ServletResponse对象传给用户所请求的Servlet,Servlet把处理好的结果写在ServletResponse中,最后Servlet容器把响应结果传给用户。

一个简单的Servlet

package com.app.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Jue extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("这是一个get");
out.close();
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("这是一个post");
out.close();
} @Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
} @Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
} }

配置Servlet的web.xml

servlet和servlet-mapping中的servlet-name要保持一致。

url-pattern即是该servlet的访问路径。

<!--servlet逻辑名-->
<servlet>
<servlet-name>Jue</servlet-name>
<servlet-class>com.app.servlet.Jue</servlet-class>
</servlet>
<!--servlet映射-->
<servlet-mapping>
<servlet-name>Jue</servlet-name>
<url-pattern>/jue.eee</url-pattern>
</servlet-mapping>

Servlet的get访问

Servlet默认的即为get访问。

在地址栏输入:http://localhost:8888/WebDemo/jue.eee

Servlet的post访问

Servlet的post访问必须建立在JSP的表单的基础上。

<%@ 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>
This is my JSP page. <br>
<form method="post" action="user.eee">
<table cellspacing="5" align="center" width="840" border-collapse="collapse">
<tr><td width="220" align="right"><img src="img/logo_login01.gif"/></tr>
<tr ><td align="center"></td><td></td></tr>
<tr ><td align="right">账 号:  </td>
<td><input type="text"id="username" name="username"style="height:25px;width:250px" vspace="5"></td></tr>
<tr><td align="right">密 码:  </td>
<td><input id="password"type="password" name="password"style="height:25px;width:250px" vspace="5"></td></tr>
<tr><td ></td><td><input type="checkbox">
<font size="1" face="微软雅黑" color="red"> 记住我一周</font></td>
<tr><td ></td><td>
<input type="submit" name="submit" value="LOGIN"/>
<a href="https://passport.csdn.net/account/forgotpassword" target="_blank" align="right">
<font size="1"><u>忘记密码</u></font></a>
</td></tr>
</table>
</form>
</body>
</html>

访问该表单所在页面:http://localhost:8888/WebDemo/MyJsp.jsp

登录后,页面即会跳转至 Servlet 的post页面 :http://localhost:8888/WebDemo/user.eee

【Servlet】Servlet应用的get、post访问及和JSP的配合使用的更多相关文章

  1. org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.servlet.Servlet

    java.lang.ClassCastException: org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.se ...

  2. sae Servlet class XXXX is not a javax.servlet.Servlet

    以前都是使用myeclipse开发web工程上传sae后没有问题,但是使用javaee导出war包上传sae 无法访问 Servlet class  XXXX is not a javax.servl ...

  3. cannot be cast to javax.servlet.Servlet

    在第一次开发Maven项目时,maven环境和仓库以及eclipse都和讲师讲解的一样,可是却遇到下面这个问题: java.lang.ClassCastException: servlet.UserS ...

  4. java.lang.ClassCastException: cn.itcase.serviceImpl.servicestudentImpl cannot be cast to javax.servlet.Servlet

    java.lang.ClassCastException: cn.itcase.serviceImpl.servicestudentImpl cannot be cast to javax.servl ...

  5. 异常:Servlet class X is not a javax.servlet.Servlet

    使用Maven命令 mvn archetype:create 创建了一个简单的web项目.导入Eclipse运行时,报这样的异常信息: Servlet class X is not a javax.s ...

  6. Servlet Servlet是Java平台上的CGI技术

    Servlet Servlet是Java平台上的CGI技术.Servlet在服务器端运行,动态地生成Web页面.与传统的CGI和许多其它类似CGI的技术相比,Java Servlet具有更高的效率并更 ...

  7. Servlet - Servlet相关

    1. 概念 Servlet是指任何实现了Servlet接口的类, Servlet运行于支持Java的应用服务器中, Servlet可以响应任何类型的请求, 但大多数情况下, Servlet只用来扩展基 ...

  8. "xxx cannot be cast to jakarta.servlet.Servlet "报错解决方式

    在做jsp的上机时候同学出现了一个500错误:com.kailong.servlet.ComputeBill cannot be cast to jaka.servlet.Servlet 然后因为我用 ...

  9. servlet servlet请求与响应

    request 客户端浏览器发出的请求被封装成一个HttpServletRequest对象.所有的信息包括请求的地址,请求的参数,提交的数据,上传的文件,客户端的Ip地址甚至客户端操作系统都包含在Ht ...

随机推荐

  1. DHTMLX 前端框架 建立你的一个应用程序 教程(六)-- 表格加载数据

    从数据库加载数据 这篇我们介绍从MySQL数据库中加载数据到表格 我们使用 MySql的数据库dhtmlx_tutorial 和表contacts 示例使用的是PHP平台和dhtmlxConnecto ...

  2. MMU、Icache、Dcache

    http://blog.csdn.net/iodoo/article/details/8954014 i-cache(instruction cache)是指令高速缓冲存储器. Cache存储体:存放 ...

  3. Java从入门到精通(一)

    Java编程可以分为三个方向 ① Java se (j2se) 桌面开发 ② Java ee (j2ee) WEB开发 ③ Java me (j2me) 手机开发 java se 是基础中的基础 Ja ...

  4. Python标准库之urllib,urllib2

    urllib模块提供了一些高级接口,用于编写需要与HTTP服务器交互的客户端.典型的应用程序包括从网页抓取数据.自动化.代理.网页爬虫等. 在Python 2中,urllib功能分散在几个不同的库模块 ...

  5. Atom 扩展离线安装

    1.下载原始包 2.解压放入atom的packages文件夹中 3.通过nodejs的npm指令进行安装 运行->cmd 4.重启Atom就ok了

  6. WordPress Kernel Theme ‘upload-handler.php’任意文件上传漏洞

    漏洞名称: WordPress Kernel Theme ‘upload-handler.php’任意文件上传漏洞 CNNVD编号: CNNVD-201311-127 发布时间: 2013-11-12 ...

  7. Linux Kernel ‘write_tag_3_packet()’函数本地基于堆的缓冲区溢出漏洞

    漏洞名称: Linux Kernel ‘write_tag_3_packet()’函数本地基于堆的缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-067 发布时间: 2013-11-07 ...

  8. zookeeper服务器端管理工具

    zookeeper基本是基于API和console进行znode的操作,并没有一个比较方便的操作界面,这里也发现了taobao 伯岩写的一个工具,可以比较方便的查询zookeeper信息. 工具的开发 ...

  9. boost库的使用(一)

    参考http://www.cnblogs.com/lexus/archive/2012/07/15/2592250.html bjam stage --without-python --toolset ...

  10. HTML5/CSS3动画应用

    http://www.html5tricks.com/cool-html5-css3-animation.html