3.1 简介
3.1.1 Servlet的功用
3.1.2 Servlet代码初探
Java Servlet是一种使用Java编写的CGI程序
CGI:通用网关接口
与传统CGI不同的是,Servlet是使用一种多线程的程序处理方式 ---性能较高
在Servlet中可以控制输出HTML代码,则所有的HTML代码都必须使用out.println();一行一行输出。
Java代码
1.package org.sky.darkness.servlet ;
2.import java.io.* ;
3.// HttpServlet属于javax.servlet.http包下
4.// ServletException属于javax.servlet包下
5.import javax.servlet.* ;
6.// HttpServletRequest、HttpServletResponse存放在javax.servlet.http包下
7.import javax.servlet.http.* ;
8.
9.public class SimpleServlet extends HttpServlet
10.{
11. // 表示处理get请求
12. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
13. {
14. PrintWriter out = resp.getWriter() ;
15. out.println("<HTML>") ;
16. out.println("<HEAD>") ;
17. out.println("<TITLE>THE FIRST SERVLET</TITLE>") ;
18. out.println("</HEAD>") ;
19. out.println("<BODY>") ;
20. out.println("<H1>Hello World!!!</H1>") ;
21. out.println("</BODY>") ;
22. out.println("</HTML>") ;
23. out.close() ;
24. }
25. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
26. {
27. this.doGet(request,response) ;
28. }
29.};
30.
31./*
32. // 表示要使用一个Servlet
33. <servlet>
34. // 在web.xml文件内部起作用的名字
35. <servlet-name>simple</servlet-name>
36. // Servlet程序所在的包.类名称
37. <servlet-class>org.sky.darkness.servlet.SimpleServlet</servlet-class>
38. </servlet>
39. // Servlet 映射地址
40. <servlet-mapping>
41. // 在web.xml文件内部起作用的名字,与上面一致
42. <servlet-name>simple</servlet-name>
43. // 具体的映射路径,前面必须有一个/
44. <url-pattern>/demo</url-pattern>
45. </servlet-mapping>
46.*/
package org.sky.darkness.servlet ;
import java.io.* ;
// HttpServlet属于javax.servlet.http包下
// ServletException属于javax.servlet包下
import javax.servlet.* ;
// HttpServletRequest、HttpServletResponse存放在javax.servlet.http包下
import javax.servlet.http.* ;
public class SimpleServlet extends HttpServlet
{
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
PrintWriter out = resp.getWriter() ;
out.println("<HTML>") ;
out.println("<HEAD>") ;
out.println("<TITLE>THE FIRST SERVLET</TITLE>") ;
out.println("</HEAD>") ;
out.println("<BODY>") ;
out.println("<H1>Hello World!!!</H1>") ;
out.println("</BODY>") ;
out.println("</HTML>") ;
out.close() ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
this.doGet(request,response) ;
}
};
/*
// 表示要使用一个Servlet
<servlet>
// 在web.xml文件内部起作用的名字
<servlet-name>simple</servlet-name>
// Servlet程序所在的包.类名称
<servlet-class>org.sky.darkness.servlet.SimpleServlet</servlet-class>
</servlet>
// Servlet 映射地址
<servlet-mapping>
// 在web.xml文件内部起作用的名字,与上面一致
<servlet-name>simple</servlet-name>
// 具体的映射路径,前面必须有一个/
<url-pattern>/demo</url-pattern>
</servlet-mapping>
*/
注意:只要是web.xml文件被修改,一定要重起服务器
3.1.3 Jsp的作用
3.2 Servlet API
3.2.1 javax.servlet.ServletInputStream:
* public int readLine(byte[] b,
int off,
int len)
throws java.io.IOException
3.2.2 javax.servlet.ServletOutputStream
* public void print(java.lang.String s)
throws java.io.IOException
* public void println(java.lang.String s)
throws java.io.IOException
3.2.3 javax.servlet.ServletRequest(Interface)
* public java.lang.Object getAttribute(java.lang.String name)
* public java.util.Enumeration getAttributeNames()
* public java.lang.String getParameter(java.lang.String name)
* public java.util.Enumeration getParameterNames()
* 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
3.2.4 javax.servlet.ServletResponse(Interface)
* public java.io.PrintWriter getWriter()
throws java.io.IOException
3.2.5 javax.servlet.http.HttpServletRequest(Interface)
* public java.lang.String getContextPath()
* public Cookie[] getCookies()
* public java.lang.String getHeader(java.lang.String name)
* public HttpSession getSession()
3.2.6 javax.servlet.http.HttpServletResponse(Interface)
* public void addCookie(Cookie cookie)
* public void sendRedirect(java.lang.String location)
throws java.io.IOException
3.2.7 javax.servlet.ServletConfig(Interface)
* public java.lang.String getInitParameter(java.lang.String name)
* public java.util.Enumeration getInitParameterNames()
* public ServletContext getServletContext()
3.2.8 javax.servlet.ServletContext(Interface)
* public java.lang.Object getAttribute(java.lang.String name)
* public java.util.Enumeration getAttributeNames()
* public ServletContext getContext(java.lang.String uripath)
* public RequestDispatcher getRequestDispatcher(java.lang.String path)
* public void setAttribute(java.lang.String name,
java.lang.Object object)
3.3 Servlet 的生命周期
3.3.1 生命周期的3 个方法
JDK提供的是JAVA SE包,而JAVAEE包不在JAVASE中,需要单独配置
Servlet生命周期:
1、 初始化1次:默认情况下:在第一次使用Servlet程序时初始化
<load-on-startup>1</load-on-startup>
关于Servlet初始化方法有两种:
? init()
? init(ServletConfig config):当使用此初始化时,上面的无参无效
2、 服务(doGet、doPost):直接输入的就是doGet请求
3、 销毁(1次):
? 服务器关闭
? 次Servlet长时间不使用
对于一个基本的Servlet程序必须注意以下几点:
? 符合Servlet结构
? 在web.xml文件中配置
Java代码
1.package org.sky.darkness.servlet ;
2.import java.io.* ;
3.import javax.servlet.* ;
4.import javax.servlet.http.* ;
5.
6.public class LifeCycleServlet extends HttpServlet {
7. // 初始化
8. public void init() throws ServletException {
9. System.out.println("** Servlet 初始化 ...") ;
10. }
11. /*
12. // 初始化
13. public void init(ServletConfig config) throws ServletException {
14. System.out.println("** ServletConfig 初始化 ...") ;
15. }
16. */
17. // 表示处理get请求
18. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException {
19. System.out.println("** Servlet doGet处理 ...") ;
20. }
21. // 处理post请求
22. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException {
23. System.out.println("** Servlet doPost处理 ...") ;
24. }
25. // 销毁
26. public void destroy() {
27. System.out.println("** Servlet 销毁 ...") ;
28. }
29.};
30.
31./*
32. <servlet>
33. <servlet-name>life</servlet-name>
34. <servlet-class> org.sky.darkness.servlet.LifeCycleServlet</servlet-class>
35. </servlet>
36.
37. <servlet-mapping>
38. <servlet-name>life</servlet-name>
39. <url-pattern>/lc</url-pattern>
40. </servlet-mapping>
41.*/
package org.sky.darkness.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class LifeCycleServlet extends HttpServlet {
// 初始化
public void init() throws ServletException {
System.out.println("** Servlet 初始化 ...") ;
}
/*
// 初始化
public void init(ServletConfig config) throws ServletException {
System.out.println("** ServletConfig 初始化 ...") ;
}
*/
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException {
System.out.println("** Servlet doGet处理 ...") ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException {
System.out.println("** Servlet doPost处理 ...") ;
}
// 销毁
public void destroy() {
System.out.println("** Servlet 销毁 ...") ;
}
};
/*
<servlet>
<servlet-name>life</servlet-name>
<servlet-class> org.sky.darkness.servlet.LifeCycleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>life</servlet-name>
<url-pattern>/lc</url-pattern>
</servlet-mapping>
*/
3.3.2 生命周期的各个阶段
3.4 HTTP Servlet 的基础知识
3.5 用MyEclipse开发Servlet应用程序
初始化:
Java代码
1.package org.sky.darkness.servlet ;
2.import java.io.* ;
3.import javax.servlet.* ;
4.import javax.servlet.http.* ;
5.
6.public class InitParameterServlet extends HttpServlet
7.{
8. // 初始化
9. // 要取得初始化参数,必须使用以下初始化方法
10. public void init(ServletConfig config) throws ServletException
11. {
12. // config对象中有取得初始化参数的方法:getInitParameter("参数名称")
13. String name = config.getInitParameter("name") ;
14. String password = config.getInitParameter("password") ;
15. String dd = config.getInitParameter("DBDRIVER") ;
16.
17. System.out.println("name => "+name) ;
18. System.out.println("password => "+password) ;
19. System.out.println("DBDRIVER => "+dd) ;
20. }
21.
22. // 表示处理get请求
23. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
24. {
25. // System.out.println("** Servlet doGet处理 ...") ;
26. }
27. // 处理post请求
28. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
29. {
30. // System.out.println("** Servlet doPost处理 ...") ;
31. }
32. // 销毁
33. public void destroy()
34. {
35. // System.out.println("** Servlet 销毁 ...") ;
36. }
37.};
38.
39./*
40. <servlet>
41. <servlet-name>param</servlet-name>
42. <servlet-class> org.sky.darkness.servlet.InitParameterServlet</servlet-class>
43. <load-on-startup>0</load-on-startup>
44. <init-param>
45. <param-name>name</param-name>
46. <param-value>darkness</param-value>
47. </init-param>
48. <init-param>
49. <param-name>password</param-name>
50. <param-value>wind</param-value>
51. </init-param>
52. <init-param>
53. <param-name>DBDRIVER</param-name>
54. <param-value>oracle.jdbc.driver.OracleDriver</param-value>
55. </init-param>
56. </servlet>
57.
58. <servlet-mapping>
59. <servlet-name>param</servlet-name>
60. <url-pattern>/ipar</url-pattern>
61. </servlet-mapping>
62.*/
package org.sky.darkness.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class InitParameterServlet extends HttpServlet
{
// 初始化
// 要取得初始化参数,必须使用以下初始化方法
public void init(ServletConfig config) throws ServletException
{
// config对象中有取得初始化参数的方法:getInitParameter("参数名称")
String name = config.getInitParameter("name") ;
String password = config.getInitParameter("password") ;
String dd = config.getInitParameter("DBDRIVER") ;
System.out.println("name => "+name) ;
System.out.println("password => "+password) ;
System.out.println("DBDRIVER => "+dd) ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doGet处理 ...") ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
// System.out.println("** Servlet doPost处理 ...") ;
}
// 销毁
public void destroy()
{
// System.out.println("** Servlet 销毁 ...") ;
}
};
/*
<servlet>
<servlet-name>param</servlet-name>
<servlet-class> org.sky.darkness.servlet.InitParameterServlet</servlet-class>
<load-on-startup>0</load-on-startup>
<init-param>
<param-name>name</param-name>
<param-value>darkness</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>wind</param-value>
</init-param>
<init-param>
<param-name>DBDRIVER</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>param</servlet-name>
<url-pattern>/ipar</url-pattern>
</servlet-mapping>
*/
servlet文件夹下的jsp提交:
Java代码
1.<form action="../formServlet" method="post">
2.用户名:<input type="text" name="uname">
3.<input type="submit" value="提交">
4.</form>
5.----------------------------或--------------------------------------
6.<servlet>
7. <servlet-name>form</servlet-name>
8. <servlet-class>org.sky.darkness.servlet.FormServlet</servlet-class>
9. </servlet>
10. <servlet-mapping>
11. <servlet-name>form</servlet-name>
12. <url-pattern>/servlet/formServlet</url-pattern>
13. </servlet-mapping>
<form action="../formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>
----------------------------或--------------------------------------
<servlet>
<servlet-name>form</servlet-name>
<servlet-class>org.sky.darkness.servlet.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>form</servlet-name>
<url-pattern>/servlet/formServlet</url-pattern>
</servlet-mapping>
ServletContext对象如何取得:
? init():不写,直接取得
ServletContext app = this.getServletContext() ;
? init(ServletConfig config):通过config对象取得:
ServletContext app = this.config.getServletContext() ;
Java代码
1.package org.sky.darkness.servlet ;
2.import java.io.* ;
3.import javax.servlet.* ;
4.import javax.servlet.http.* ;
5.
6.public class FormServlet extends HttpServlet
7.{
8. private ServletConfig config = null ;
9. public void init(ServletConfig config) throws ServletException
10. {
11. this.config = config ;
12. }
13. // 表示处理get请求
14. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
15. {
16. // System.out.println("** Servlet doGet处理提交参数 ...") ;
17. this.doPost(req,resp) ;
18. }
19. // 处理post请求
20. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
21. {
22. String name = req.getParameter("uname") ;
23. // 取得application对象
24. // ServletContext app = this.getServletContext() ;
25. ServletContext app = this.config.getServletContext() ;
26. app.setAttribute("addr","sky") ;
27. // 取得一个session对象
28. HttpSession session = req.getSession() ;
29. session.setAttribute("sname",name) ;
30. // System.out.println("** Servlet doPost处理提交参数 ...") ;
31. System.out.println("name = "+name) ;
32. // 重定向
33. resp.sendRedirect("demo.jsp") ;
34. }
35.};
36.
37./*
38. <servlet>
39. <servlet-name>form</servlet-name>
40. <servlet-class>org.sky.darkness.servlet.FormServlet</servlet-class>
41. </servlet>
42. <servlet-mapping>
43. <servlet-name>form</servlet-name>
44. <url-pattern>/formServlet</url-pattern>
45. </servlet-mapping>
46.*/
47.
48.-------------form.jsp--------------
49.<form action="formServlet" method="post">
50.用户名:<input type="text" name="uname">
51.<input type="submit" value="提交">
52.</form>
53.------------demo.jsp-------------
54.<h1><%=session.getAttribute("sname")%></h1>
55.
56.<h1><%=getServletContext().getAttribute("addr")%></h1>
57.<h1><%=application.getAttribute("addr")%></h1>