java web高级编程 笔记1
chapter1:了解web应用程序
web应用程序主要组件:
- Servlet
- 过滤器
- 监听器
- JSP
chapter2:各类web容器介绍
略
chapter3:Servlet介绍
Servlet是一个运行在Web服务器中的Java小程序,Servlet将会接受和响应来自Web客户端的请求,使用HTTP(超文本传输协议)进行通信;
public class HelloServlet extends HttpServlet{ @Override public void init() throws ServletException { System.out.println(getServletName()+"初始化!"); super.init(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(getServletName()+"doGet方法"); resp.getWriter().println(getServletName()+":Hello,World!"); } private void destroy() { System.out.println(getServletName()+"销毁!");
super.destroy(); } }
web.xml:
<servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>com.mryangpo.HelloServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>/greeting</url-pattern> </servlet-mapping> <servlet> <servlet-name>helloServlet1</servlet-name> <servlet-class>com.mryangpo.HelloServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>helloServlet1</servlet-name> <url-pattern>/greeting1</url-pattern> </servlet-mapping>
<load-on-startup>1</load-on-startup>表示web容器在应用程序启动的时候就加载Servlet(实例化并调用其init()方法);
关闭服务时会调用destroy方法,在doGet会service方法中调用destroy方法,访问一次servlet后仍可以继续访问;
servlet执行过程:初始化一次,进入service方法中获取method类型,根据类型进入doGet或其他方法,服务器关闭,调用destroy方法。
了解doGet,doPost等方法:
getRequestURL
getRequestURI
使用初始化参数配置应用程序:
上下文初始化参数:
<context-param> <param-name>settingone</param-name> <param-value>one</param-value> </context-param>
在Servlet类中获取上下文:
ServletContext servletContext = this.getServletContext();
String param1=servletContext.getInitParameter("settingone")
servlet初始化参数:
<servlet> <servlet-name>helloServlet1</servlet-name> <servlet-class>com.mryangpo.HelloServlet</servlet-class> <init-param> <param-name>settingtwo</param-name> <param-value>two</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>helloServlet1</servlet-name> <url-pattern>/greeting1</url-pattern> </servlet-mapping>
在Servlet类中获取初始化参数:
ServletConfig servletConfig = this.getServletConfig();
String settingtwo=servletConfig.getInitParameter("settingtwo");
chapter4 JSP(JavaServer Pages) 的使用
jsp编译时机:
<!-- Html/xml注释 -->
<%-- jsp注释 -->
chapter5 会话的使用
略
chapter12 Spring Framework