post请求能不能像get请求一样,在路径后面加参数?

参数的方法不推荐,涉及安全问题,最好不用。

实验:

1、Servlet

package com.wrox;

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;

@WebServlet(
        name = "helloServlet",
        urlPatterns = {"/greeting", "/salutation", "/wazzup"},
        loadOnStartup = 1
)
public class HelloServlet extends HttpServlet {
    private static final String DEFAULT_USER = "Guest";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String user = request.getParameter("user");
        String returnTo = request.getParameter("returnTo");
        if (user == null)
            user = HelloServlet.DEFAULT_USER;

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        PrintWriter writer = response.getWriter();
        writer.append("\r\n")
                .append("\r\n")
                .append("    \r\n")
                .append("        Hello User Application\r\n")
                .append("    \r\n")
                .append("    \r\n")
                .append("        Hello, ").append(user).append(" - ").append(returnTo).append("!

\r\n") .append("
\r\n") .append(" Enter your name:
\r\n") .append("
\r\n") .append(" \r\n") .append("
\r\n") .append(" \r\n") .append("\r\n"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }

2、页面Jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

    
        Hello, User Application Index File
    
    
        Go to greeting.
    



3、结果
post请求能不能像get请求一样,在路径后面加参数?_第1张图片

小记:

慢慢探索、深入,争取写出点有意义的东西。有疑问,就写个小栗子,搞一下就明白了。

你可能感兴趣的:(JavaWeb)