
本文共 5308 字,大约阅读时间需要 17 分钟。
Servlet���JSP������������
Servlet������������
���������Servlet���������������������@.WebServlet
���������������������������������������������������������web.xml
������������������������
@WebServlet("/your/url")
���������������������������������web.xml
���������metadata-complete="false"
������������������������
Servlet������������
������������
������������������������
���������Forward���
���������������������������������������������������������������������������������
request.getRequestDispatcher("/path").forward(request, response);
���������
- ������������������������������������������������
- ���������
WEB-INF
������������������������������
������������Redirect���
������������HTTP������������������������������������������������
response.sendRedirect("/new/path");
���������
- ������������������������������������������������
- ������������������������������������������������������
- ������������������������������
���������������������������
������
- ������������������������������������������
- ������������������������������������Servlet������
Request
���������
���������
- ���������������������������������������������������������������
- ���������������������������������������������������
���������������
���������������
������ | ������������ | ������ |
---|---|---|
PageContext | PageContext | ������������ |
HttpRequest | HttpServletRequest | ������������������ |
HttpSession | HttpSession | ��������������������� |
ServletContext | ServletContext | ������������ |
������������
- ���������������
super.getServletContext()
- ������ServletConfig���
super.getServletConfig().getServletContext()
- ������Request���
request.getServletContext()
- ������Session���
request.getSession().getServletContext()
������������
- ���������������
String path = getServletContext().getContextPath();
- ���������������������
String realPath = getServletContext().getRealPath("/������");
- ������������������������
String param = getServletContext().getInitParameter("key");
������������
- ���������
request.setAttribute("key", value)
- ���������
request.getAttribute("key")
������
public class ScopeServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // ������������������������������ Integer reqCount = (Integer) req.getAttribute("reqCount"); if (reqCount == null) { reqCount = 1; } else { reqCount++; } req.setAttribute("reqCount", reqCount); // ������������������������������������ HttpSession session = req.getSession(); Integer sesCount = (Integer) session.getAttribute("sesCount"); if (sesCount == null) { sesCount = 1; } else { sesCount++; } session.setAttribute("sesCount", sesCount); // ������������������������������ ServletContext sc = req.getServletContext(); Integer appCount = (Integer) sc.getAttribute("appCount"); if (appCount == null) { appCount = 1; } else { appCount++; } sc.setAttribute("appCount", appCount); // ������������ try (PrintWriter out = res.getWriter()) { out.println("���������������������" + reqCount); out.println("���������������������" + sesCount); out.println("���������������������" + appCount); } }}
JSP������������
JSP������
- ���������Java���������������������������SQL���HTML���������������������
- ���������
- ���Servlet���������������
- ������JSP���������������������
JSP���HTML������
- ���������������
.jsp
vs.html
- ���������������������������JSP directives������
<%@ page ... %>
JSP������
������������
- ���������
<!-- Java������������ -->
- ���������������
<%= data %>
- Java���������������
<% code %>
- ������������������������
<%![...]
������JSP������
Page������
���������������������������������������������������
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8" %>
Include������
������������������������������������������������
Taglib������
������������������������������������
<%-- ������������������������������������������ --%>
JSP������������
������������ | ������ | ������������ |
---|---|---|
application | ServletContext | ��������������������� |
session | HttpSession | ������������������������ |
request | HttpServletRequest | ������������������������ |
pageContext | PageContext | ��������������������������� |
response | HttpServletResponse | ������������������������ |
out | JspWriter | ���������������� |
config | ServletConfig | Servlet������������ |
page | this | ������JSP������������ |
exception | ��� | ������������������ |
JSP���������
���������������
��������������� | ������ | ������������ |
---|---|---|
application | ServletContext | ��������������� |
session | HttpSession | ������������ |
request | HttpServletRequest | ������������ |
pageContext | PageContext | ������������ |
������������
JSP������ ������������������:<%= request.getAttribute("count") %>
JSP������������
jsp:include
���������������������������������������������������������������������
���������������������Servlet���JSP���������������������������������������������������������������������������������
发表评论
最新留言
关于作者
