֪

ҳ
ǰλ: ҳ > > Jspʵ̳ >

sevletѧϰժҪ

ʱ:2015-01-30 19:22Դ:֪www.zhixing123.cn ༭:

1. Servlet
2. ʲôservlet:webеķJAVAҪӦHTTPServletһMVCеĿ֡
3. ʲôservlet:servletڵӦó磨tomcat
4. servletܣ
public class servlet extendsHttpServlet{
public void init() throws ServletException {}//ʼ
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{}//doget
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{}//dopost
public void destroy() {}//servlet
}
5. дHello Servlet
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Response.setContentType ("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("Hello");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response)
}
}
6. servletļ̳йϵ


7. servletservlet APIĹϵ
8. ʲôJSP Page
java server page servletչǿwebҳһservlet
9. servletJSPʲôص㣬ѡּ
10. JSPģ
Model1jsp άѣɸԲ
Model2jsp+javabean jspʹusebeanǩjspбͬModel1
Mvcjsp+servlet+javabean

11. JSPǩ
ǩjsp:include
jsp:forword
jsp:usebean
jsp:set/get Property
ָǩ<%@page……%>
<%@include…..%>
<%@taglib…..%>
12. servlet
1) HTMLлò
request.getParameterNames();enumer;
request.getParameter(); String
2) λͷϢ
Enumeration enumer=request.getHeaderNames();
While(enumer. HasMoreElements())
{
String header= enumer.next();
String result=request.getHeader(header);
Out.print(result);
}
3) λÿͻcookieϢ
request.getCookies();array[]
4) responseͷϢ
response.setHeader();
5) Ӧcontent type
response.setContentType(“text/html charset=utf-8”);
6) λI/Otext
7) δӵǰservletضURL
response.sendRedirect(“url”)
RequstDispatcher rd=request.get RequstDispatcher(“url”)
Rd.forword(request,response);
8) ͻдcookie
Cookie cookie=new Cookie(“object”,”jklj”);
Cookie.setMaxAge(ʱ);
Response.addCookie(cookie);
13. servletڣ˭servletڣservletJ2EE淶
ʵһservletinit󵽴ʱserviceservice͵ãdoGetdoPostʵʱdestory
14. δRequest Dispatcher,νתwebԴwebԴrequest״̬Լ״̬ʲô
RequstDispatcher rd=request.get RequstDispatcher(“url”)
Rd.forword(request,response);
Request.setAttribute(“object”,””);
һӦrequestھͽ
15. web.xmlservlet

<servlet>
<servlet-name>name</servlet-name>
<servlet-class>package</servlet-class>
</servlet>
ӳ
<servlet-mapping>
<servlet-name>name</servlet-name>
<url-patten>/patten</url-patten>
</servlet-mapping>
16. ServletConfig
ServletConfig sc=getServletConfig();
17. λservletContext
servletContext sc=getservletContext();
18. <html><body>
<a href=”/servlet/HelloServlet”>POST</a>
</body></html>
servletʱdoPostdoGetdoFormdoHrefеĸ
doGet
19. doGet
PrintWriter out = res.getWriter();
Out.println(“<html><body>Hello</body></html>”);
If!res.isCommitted жǷresponseύ
Ifreg.getParameter”name”==null
{
res.sendErrorHttpServletResponse.SC_UNAUTHORIZED;
}
20. ˵µwebԴӦ÷webеʲôĿ¼£̬ҳJSPservlet classweb.xmlTag librariesJAR Filejava classԼҪԴļ
tomcat
web.xml, Tag libraries: \webapps\\WEB-INF
servlet,javaclass: \webapps\\WEB-INF\classes \
jar: \webapps\\WEB-INF\lib\
21. δwar
webĿѹrarļȻ󽫺׺Ϊwar
22. ServletContextóʼ
servletContext sc=getservletContext();
String s=sc.getInitParameter(key);
23. ļServletContextʼ
<init-param>
<param-name>name</param-name>
<param-value>vlaue</param-value>
</init-param>
24. дServletContextԼȡ
servletContext sc=getservletContext();
sc.setAttribute(key,value);
25. request session web application𣬴ÿscope
ڲͬrequestӦʱڽsessionweb.xmlʱ䡢ֶwebڽWebapplicationֶ
26. listener
27. дԼfilter
28. һrequestresponseİװ
29. дһservlet룬sessionԼsessionȡö
HttpSession session=request.getSession();
session.setAttribute(key,value);
30. sessionAPI
31. ˵session
sessionweb.xmlʱ䡢ֶwebڽ
32. дһδӦsessionʱ¼
33. webʹʲôsession
34. ʹsessionʧЧ
sessionweb.xmlʱ䡢ֶwebڽ
35. sessionͻBirding̼
36. client֧cookieʱʹsession
37. ʲôAuthenticationʶûʶû ˭ʲôAuthorizationȨʶûʲôȨޡݵԣύݣݣݿݡݵĻԣܣHTTPS
38. JSPָǩʽ
JSPǩʽ
JSPдαǩʽ
JSPжǩʽ
JSPעͱǩʽ
JSPеıʽǩʽ
39. ָǩЩ
40. actionǩЩ
41. page
42. <%!int i;%>→Ա
<%int j;%>→ֲ
<%=i++%><%=j++%>
43. JSPеö
44. <%@ include file=” ”%><jsp:include page=” ”/>ͬ
45. jsp:useBeanid,scope,type,type,classʹ

------ָ----------------------------
ǩ(Tag):sevletѧϰ
------ָ----------------------------
Ƽ
Ȥ