Servlet接收用户表单参数

Servlet接收用户表单参数,第1张

1.自定义Servlet
package com.zhan;


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


public class FileServlet extends HttpServlet {

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


        String username = req.getParameter("username");//接收参数

        System.out.println(username);//验证是否接收成功

        req.getRequestDispatcher("success.jsp").forward(req,resp);//转发页面
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         doGet(req, resp);
    }
}
2.编写前端页面(JSP)
<%@page contentType="text/html; charset=UTF-8" language="java" %>

 
     
         $Title$
     
 


   

注意action中填写的应该是第一步自定义的FileServlet,表示提交该页面的数据到此Servlet中

3.在web.xml中配置FileServlet



  Archetype Created Web Application
  
  
    FileServlet
    com.zhan.FileServlet
  
 
   FileServlet
   /FileServlet
 
  
  

4.结果

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/799216.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存