首页 > 代码库 > Jsp,Servlet初学总结

Jsp,Servlet初学总结

一、Jsp

  1. 指令:

    <%@page language="java" import="java.*" contextType="text/html" charset="utf-8" pageEncoding="utf-8" %>

    <%@include file="url" %>

    <%@taglib uri="" prefix="" %>

  2. 动作(部分):

    <jsp:useBean id="对象名" class="类名(绝对路径)" scope="page"/>

    <jsp:setProperty name="id" property="*"/>  //与request对象中属性名相同的属性获得相应的值

      或<jsp:setProperty name="id" property="属性名"/>

      或<jsp:setProperty name="id" property="属性名" value="http://www.mamicode.com/值"/>

    <jsp:getProperty name="id" property="属性名"/>

    <jsp:include page="url" flush="true|false"/>

    <jsp:forward page="url"/>

  3. 内置对象(部分):
    1)request

      常用方法:

        String request.getParameter(String name);

        String[] request.getParameterValues(String name);

        void request.serAttribute("name","value");

        Object request.getAttribute("name");

        void request.removeAttribute("name");

        Cookie[] request.getCookie();

    2)response

      常用方法:

        void response.addCookie(Cookie cookie);

        PrintWriter response.getWriter();

        void response.sendRedirect("url");

        void response.setHeader("refresh","3");  //3秒后刷新

        void responser.setHeader("refresh","3;url=login.jsp");  //3秒后刷新,并跳转到login.jsp

    3)session

      常用方法:

        void session.setAttribute("name","value");

        Object session.getAttribute("name");

        String session.getId();

        void session.invalidate();  //session失效

        void session.setMaxInactiveInterval(int interval);

        void session.setMaxInactiveInterval(60);  //有效时间60s

    4)application

      常用方法:

        Object application.getAttribute("name");

        void application.setAttribute("name","value");

        void application.removeAttribute("name");

 

二、Servlet

  1. 框架:

    package cn.edu.servlet;

 

    import java.io.IOException;

    import java.io.PrintWriter;

    import javax.servlet.http.HttpServlet;

 

    public class FirstServlet extends HttpServlet{

      private static final long seriaVersionUID = 1L;

      public FirstServlet(){

        super();

      }

      protected void doGet(HttpServeltRequest request,HttpServletResponse response)throws ServletException,IOException{

        response.setContextType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();

 

        String username = request.getParameter("username");

        String password = request.getParameter("password");

        out.print();

        ······

      }

      protected void doPost(HttpServeltRequest request,HttpServletResponse response)throws ServletException,IOException{

        doGet(request,response);

      }

    }

 

  2. 重定向 & 请求转发: 

    response.sendRedirect("url");

 

    RequestDispatcher rd= request.getRequestDispatcher("url");

    rd.forward(request,response);

  3. Cookie & Session

    1)Cookie 的创建和使用

      Cookie cookie = new Cookie("name","value");

      response.addCookie(cookie);

      Cookie[] cookies = request.getCookie();

      

      void cookie.setMaxAge(60);  //cookie有效时间60s

      String cookie.getName();

      String cookie.getValue();

    2)Session 的创建和使用

      HttpSession session = request.getSession();

      session.setAttribute("name","value");

      session.getAttribute("name");

      session.removeAttribute("name");

      session.setMaxInactiveInterval(60);  //session有效时间60s

 

        

 

Jsp,Servlet初学总结