–为什么需要动态创建Web页面?
–Servlet 的功能
–Servlet 相对“传统” CGI的优势
–Servlet 的相关类和接口
–Servlet 实例
• “HelloWorld” Servlet
–Servlet 的功能
–Servlet 相对“传统” CGI的优势
–Servlet 的相关类和接口
–Servlet 实例
• “HelloWorld” Servlet
######################Michael分割线################
• 为什么需要动态创建Web页面?
–Web页面基于用户提交的数据
• 例如,搜索引擎的结果和在线商店的订单确认信息
–Web 页面的数据频繁改变
• 例如,天气预报和新闻头条
–页面信息来自数据库
• 例如,电子商务网站的每日销售商品的价格清单
–Web页面基于用户提交的数据
• 例如,搜索引擎的结果和在线商店的订单确认信息
–Web 页面的数据频繁改变
• 例如,天气预报和新闻头条
–页面信息来自数据库
• 例如,电子商务网站的每日销售商品的价格清单
• Servlet 的功能
–读取客户端发来的显示信息(表单数据)
–读取客户端发来的隐式信息(请求头信息)
–生成相应结果
–发送显示信息给客户端(HTML)
–发送隐式数据到客户端(状态码和响应头信息)
–读取客户端发来的显示信息(表单数据)
–读取客户端发来的隐式信息(请求头信息)
–生成相应结果
–发送显示信息给客户端(HTML)
–发送隐式数据到客户端(状态码和响应头信息)
• Servlet 相对“传统”CGI的优势
–效率高
• 线程取代操作系统的进程
–方便
• 大量高级工具类
–功能强大
• 共享数据,数据持久性
–轻便
• 可以运行在所有操作系统上
–廉价
• 有大量的免费或价格较低的服务器
–安全
• 没有缓存溢出问题
–主流
–效率高
• 线程取代操作系统的进程
–方便
• 大量高级工具类
–功能强大
• 共享数据,数据持久性
–轻便
• 可以运行在所有操作系统上
–廉价
• 有大量的免费或价格较低的服务器
–安全
• 没有缓存溢出问题
–主流
• Servlet 的相关类和接口
– 包结构
• javax.servlet
• javax.servlet.http
– 包结构
• javax.servlet
• javax.servlet.http
– Servlet 接口
• public void init(ServletConfig config) throws ServletException
• public ServletConfig getServletConfig()
• public void service(ServletRequest req,ServletResponse res) throws
ServletException,java.io.IOException
• public java.lang.String getServletInfo()
• public void destroy()
• public void init(ServletConfig config) throws ServletException
• public ServletConfig getServletConfig()
• public void service(ServletRequest req,ServletResponse res) throws
ServletException,java.io.IOException
• public java.lang.String getServletInfo()
• public void destroy()
– ServletConfig
• public java.lang.String getServletName()
• public java.lang.String getInitParameter(java.lang.String name)
• public java.util.Enumeration getInitParameterNames()
• public java.lang.String getServletName()
• public java.lang.String getInitParameter(java.lang.String name)
• public java.util.Enumeration getInitParameterNames()
– ServletContext
• public ServletContext getContext(java.lang.String uripath)
• public RequestDispatcher getRequestDispatcher(java.lang.String path)
• public void log(java.lang.String msg)
• public java.lang.String getRealPath(java.lang.String path)
• public java.lang.String getInitParameter(java.lang.String name)
• public java.lang.Object getAttribute(java.lang.String name)
• public ServletContext getContext(java.lang.String uripath)
• public RequestDispatcher getRequestDispatcher(java.lang.String path)
• public void log(java.lang.String msg)
• public java.lang.String getRealPath(java.lang.String path)
• public java.lang.String getInitParameter(java.lang.String name)
• public java.lang.Object getAttribute(java.lang.String name)
– ServletRequest
• public void setAttribute(java.lang.String name,java.lang.Object o)
• public void setCharacterEncoding(java.lang.String env) throws java.io.UnsupportedEncodingException
• public java.lang.String getParameter(java.lang.String name)
• public java.lang.String[] getParameterValues(java.lang.String name)
• public void setAttribute(java.lang.String name,java.lang.Object o)
• public void setCharacterEncoding(java.lang.String env) throws java.io.UnsupportedEncodingException
• public java.lang.String getParameter(java.lang.String name)
• public java.lang.String[] getParameterValues(java.lang.String name)
– ServletResponse
• public ServletOutputStream getOutputStream() hrows java.io.IOException
• public java.io.PrintWriter getWriter() throws java.io.IOException
• public ServletOutputStream getOutputStream() hrows java.io.IOException
• public java.io.PrintWriter getWriter() throws java.io.IOException
– GenericServlet
• public abstract class GenericServlet
• extends java.lang.Object
• implements Servlet, ServletConfig, java.io.Serializable
• public abstract class GenericServlet
• extends java.lang.Object
• implements Servlet, ServletConfig, java.io.Serializable
– HttpServlet
• public abstract class HttpServlet
• extends GenericServlet
• implements java.io.Serializable
• public abstract class HttpServlet
• extends GenericServlet
• implements java.io.Serializable
– HttpServletRequest
• public interface HttpServletRequest
• extends ServletRequest
• public interface HttpServletRequest
• extends ServletRequest
– HttpServletResponse
• public interface HttpServletResponse
• extends ServletResponse
• public interface HttpServletResponse
• extends ServletResponse
– HttpSession
• public void setAttribute(java.lang.String name,java.lang.Object value)
• public void invalidate()
• public void setAttribute(java.lang.String name,java.lang.Object value)
• public void invalidate()
• Servlet 实例
–“HelloWorld” Servlet
• 创建一个HelloWorldServlet类
–编写一个类“HelloWorldServlet”继承HttpServlet
–覆盖HttpServlet中的doGet方法
–在doGet方法中将“Hello World Servlet”输出
–“HelloWorld” Servlet
• 创建一个HelloWorldServlet类
–编写一个类“HelloWorldServlet”继承HttpServlet
–覆盖HttpServlet中的doGet方法
–在doGet方法中将“Hello World Servlet”输出
• 声明、映射Servlet
–声明Servlet
–声明Servlet
• 调用Servlet方式
–URL
–提交表单
–超级连接
–JavaScript脚本
–URL
–提交表单
–超级连接
–JavaScript脚本
• 运行结果
######################Michael分割线################
URL调用Servlet方法
创建一个MyServlet类 MyServlet.java
package com.michael.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 MyServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("My blog is http://redking.blog.51cto.com");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
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 MyServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("My blog is http://redking.blog.51cto.com");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
申明、映射Servlet
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 申明Servlet -->
<servlet>
<servlet-name>王乾De技术Blog[爱生活,爱学习]</servlet-name>
<servlet-class>com.michael.servlet.MyServlet</servlet-class>
</servlet>
<!-- 映射Servlet -->
<servlet-mapping>
<servlet-name>王乾De技术Blog[爱生活,爱学习]</servlet-name>
<url-pattern>/servlet/michael.do</url-pattern>
</servlet-mapping>
</web-app>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 申明Servlet -->
<servlet>
<servlet-name>王乾De技术Blog[爱生活,爱学习]</servlet-name>
<servlet-class>com.michael.servlet.MyServlet</servlet-class>
</servlet>
<!-- 映射Servlet -->
<servlet-mapping>
<servlet-name>王乾De技术Blog[爱生活,爱学习]</servlet-name>
<url-pattern>/servlet/michael.do</url-pattern>
</servlet-mapping>
</web-app>
测试
######################Michael分割线################
提交表单调用Servlet方法
增加一个login.html页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/Servlet_Info/servlet/michael.do" method="post">
<table border="0">
<tr>
<td>帐号:</td>
<td><input type="text" name="user" id="login"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/Servlet_Info/servlet/michael.do" method="post">
<table border="0">
<tr>
<td>帐号:</td>
<td><input type="text" name="user" id="login"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
测试
######################Michael分割线################
超链接调用Servlet方法
测试
######################Michael分割线################
JavaScript脚本调用Servlet方式
login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function invoke(){
var myForm = document.getElementById("michaelForm");
myForm.action = "/Servlet_Info/servlet/michael.do";
myForm.submit();
}
</script>
</head>
<body>
<a href="/Servlet_Info/servlet/michael.do">click me!!!</a>
<form id="michaelForm" action="/Servlet_Info/servlet/michael.do" method="post">
<table border="0">
<tr>
<td>帐号:</td>
<td><input type="text" name="user" id="login"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"></td>
<input type="button" value="JS Submit" onclick="invoke()"><br><br>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function invoke(){
var myForm = document.getElementById("michaelForm");
myForm.action = "/Servlet_Info/servlet/michael.do";
myForm.submit();
}
</script>
</head>
<body>
<a href="/Servlet_Info/servlet/michael.do">click me!!!</a>
<form id="michaelForm" action="/Servlet_Info/servlet/michael.do" method="post">
<table border="0">
<tr>
<td>帐号:</td>
<td><input type="text" name="user" id="login"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"></td>
<input type="button" value="JS Submit" onclick="invoke()"><br><br>
</tr>
</table>
</form>
</body>
</html>
测试
######################Michael分割线################
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/167478,如需转载请自行联系原作者