JSP/Servlet应用:笔记

1、JSP页面引用CSS和JS文件。

2、Servlet中页面跳转URL的写法。

3、Servlet过滤器(Filter)不过滤登录页面、JS和CSS文件。

4、一个Servlet类处理多个方法。

5、Servlet返回字符串或JSON内容。

 

1、JSP页面引用CSS和JS文件


说明:${ }是EL表达式语言,通过EL的隐含对象 pageContext 获取页面上下文对象为pageContext,用于访问JSP内置对象(例如request、response、out、session、exception和page等,但不能用于获取application、config和pageContext对象)和servletContext。在获取到这些内置对象后,就可以获取其属性值。

 

2、Servlet中页面跳转URL的写法

response.sendRedirect(request.getContextPath() + "/User/UserList.jsp");

HttpServletRequest接口的 getContextPath() 方法返回请求的上下文路径,此路径以“/”开头。

 

3、Servlet过滤器(Filter)不过滤登录页面、JS和CSS文件

// 过滤处理方法
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
{
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    String servletPath = httpServletRequest.getServletPath();  //获取客户端所请求的脚本文件的文件路径
    //不过滤处理:登录页面、JS和CSS文件
    if (!servletPath.equals("/login.jsp") && !servletPath.equals("/servlet/LoginServlet")
            && !servletPath.endsWith(".js") && !servletPath.endsWith(".css"))
    {
        //省略校验代码...
    }

    // 传递给下一过滤器
    chain.doFilter(request, response);
}

 

4、一个Servlet类处理多个方法

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户Servlet类
 * @author pan_junbiao
 */
@WebServlet(name = "UserServlet", urlPatterns = "/servlet/UserServlet")
public class UserServlet extends HttpServlet
{
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        //获取参数,识别用户想请求的方法
        String methodName = request.getParameter("method");
        if (methodName == null || methodName.isEmpty())
        {
            throw new RuntimeException("没有传递method参数");
        }

        switch (methodName)
        {
            case "list":
            {
                getUserList(request, response);
                break;
            }
            case "detail":
            {
                getUserDetail(request, response);
                break;
            }
            default:
                break;
        }
    }

    /**
     * 获取用户列表
     */
    public void getUserList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // 忽略中间代码...
    }

    /**
     * 获取用户详情
     */
    public void getUserDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // 忽略中间代码...
    }
}

 

5、Servlet返回字符串或JSON内容

5.1 Servlet返回字符串

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
 
/**
 * 输出字符串Servlet类
 * @author pan_junbiao
 */
@WebServlet(name="CharacterServlet",urlPatterns = "/Servlet/CharacterServlet")
public class CharacterServlet extends HttpServlet
{
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String result = "您好,欢迎访问 pan_junbiao的博客!";
 
        //返回结果
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");
        // 获取PrintWriter对象
        PrintWriter out = response.getWriter();
        out.print(result);
        // 释放PrintWriter对象
        out.flush();
        out.close();
    }
}

5.2 Servlet返回JSON内容

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
 
/**
 * 输出JSON内容Servlet类
 * @author pan_junbiao
 */
@WebServlet(name="JsonServlet",urlPatterns = "/Servlet/JsonServlet")
public class JsonServlet extends HttpServlet
{
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String json = "{\n" +
                "\t\"userId\": 1,\n" +
                "\t\"userName\": \"pan_junbiao的博客\",\n" +
                "\t\"blogUrl\": \" https: //blog.csdn.net/pan_junbiao\",\n" +
                "\t\"sex\": \"男\"\n" +
                "}";
 
        //返回结果
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        // 获取PrintWriter对象
        PrintWriter out = response.getWriter();
        out.print(json);
        // 释放PrintWriter对象
        out.flush();
        out.close();
    }
}

 

 

 

 

你可能感兴趣的:(#,Java,Web,我の应用,我の原创)