JavaWeb之request的应用

1.获取请求参数。

package com.harris.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("成功进入请求");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //下面的方法是返回一个数组 如使用checkbox 多选框的时候
        String[] hobbies = req.getParameterValues("hobbies");
        System.out.println(Arrays.toString(hobbies));
        System.out.println(username+":"+password);

       // System.out.println(req.getContextPath());
       // resp.sendRedirect("/success.jsp");
         //下面写不写/都是对的,/是表示当前应用的目录 不加/就是相对路径 req.getRequestDispatcher("/success.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

2.请求转发。

req.getRequestDispatcher("/success.jsp").forward(req,resp);

JavaWeb之request的应用_java
请求转发之后,url不会变,而且请求的参数属性会添加在url后面。