Java获取网页form提交的信息

如提交以上网页的表单,在eclipse中的控制台上显示:

并且会跳转到网页


 

代码

JSP

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>获取提交的信息</title>
    </head>
    <body background="wallpaper_10.jpg">
        <div class="div1" align="center" >        
            <form action="<%=request.getContextPath() %>/getjsp.do" method="post">
                <table border="0"  >
                <tr><td><span >姓名</span></td
                    ><td>    <input type="text" name="xingming" id="xingming" value="" placeholder="请输入姓名" /></td></tr>
                <tr><td><span >手机号</span></td>
                    <td><input type="text" name="phone" id="phone" value="" placeholder="请输入手机号"/></td></tr>
                <tr><td><span>邮箱</span></td>
                    <td><input type="text" name="email" id="email" value="" placeholder="请输入邮箱"/></td></tr>
                <tr><td><span >性别</span></td>
                    <td>男<input type="radio" name="sex" id="sex" value="man" checked="checked"/>
                    女<input type="radio" name="sex" value="woman"/></td>
                </tr>
                <tr><td><span>您会</span></td>
                    <td>点赞<input type="checkbox" name="qiu" value="nb" checked="checked"/>
                        收藏<input type="checkbox" name="qiu" value="collect" checked="checked"/>
                        评论<input type="checkbox" name="qiu" value="comment" checked="checked"/>
                        关注<input type="checkbox" name="qiu" value="follow" checked="checked"/></td></tr>
                <tr><td><span>编程语言</span></td>
                    <td><select name="select">
                        <option>C#</option><option>JAVA</option><option>C++</option>        
                    </select></td></tr>
                <tr><td><span>生日</span></td>
                    <td><select name="year"><option>2000</option><option>2001</option><option>2002</option><option>2003</option><option>2004</option><option>2005</option>
                    <option>2006</option><option>2007</option><option>2008</option><option>2009</option><option>2010</option><option>2011</option><option>2012</option></select>年
                    <select name="mounth"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option>
                    <option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option></select>月
                    <select name="day"><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option>
                    <option>27</option><option>28</option><option>29</option><option>30</option></select>日</td></tr>
                <tr><td><input type="submit" value="提交"/></td></tr>
                </table>    
            </form>        
        </div>
    </body>
</html>

java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Getjsp" ,urlPatterns = "/getjsp.do")
public class Getjsp extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response) 
throws IOException,ServletException{
	System.out.println("开始从控制台打印信息");
	String xingming=request.getParameter("xingming");
	String phone=request.getParameter("phone");
	String email=request.getParameter("email");
	String sex=request.getParameter("sex");
	String select=request.getParameter("select");
	String year=request.getParameter("year");
	String mounth=request.getParameter("mounth");
	String day=request.getParameter("day");
	System.out.println("姓名是:"+xingming);
	System.out.println("电话是:"+phone);
	System.out.println("email是:"+email);
	System.out.println("性别是:"+sex);
	System.out.print("您会:");
	if(request.getParameterValues("qiu")!=null) {
		String []qiu=request.getParameterValues("qiu");
		for(int i=0;i<qiu.length;i++)System.out.print(qiu[i]+" ");
		System.out.println();
	}else {
		System.out.println("啥也不会");
	}
	System.out.println("编程语言是:"+select);
	System.out.println("您的生日是"+year+"年"+mounth+"月"+day+"日");
	response.sendRedirect("sussess.html");
}
public void doGet(HttpServletRequest request,HttpServletResponse response) 
throws IOException,ServletException{
	doPost(request, response);
}
}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>提交成功</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            overflow: hidden;
            background-color: #000;
        }
        h1{
            position: absolute;
            line-height: 50px;
            letter-spacing: 5px;
            color: #fff;
            width: 300px;
            top: 40%;
            left: 50%;
            margin-left: -200px;
            font-size: 30px;
        }
    </style>
</head>
<body>
    <h1>
      
    </h1>
    <canvas></canvas>
    <script>
        var canvas=document.querySelector("canvas");
            ctx=canvas.getContext("2d");
            w=canvas.width=window.innerWidth;
            h=canvas.height=window.innerHeight;
            var canvas2=document.createElement("canvas");
                ctx2=canvas2.getContext("2d");
                canvas2.width=100;
                canvas2.height=100;
            var a=canvas2.width/2;
        var grandient=ctx.createRadialGradient(a,a,0,a,a,a);
            grandient.addColorStop(0.025,'#fff');
            grandient.addColorStop(0.1, 'hsl(220,59%,18%)');
            grandient.addColorStop(0.025, 'hsl(220,60%,33%)');
            grandient.addColorStop(1,"transparent");
            ctx2.fillStyle=grandient;
            ctx2.beginPath();
            ctx2.arc(a,a,a,0,Math.PI*2);
            ctx2.fill();
            ctx2.closePath();
            var stars=[];
            var count=0;
            function Star(){
                this.pos=Math.floor(Math.random()* w/2-100);
                this.r=Math.floor(Math.random()*100);
                this.dx=w/2;
                this.dy=h/2;
                this.rand=Math.floor(Math.random()*360);
                this.speed=this.pos/100000;
                stars[count]=this;
                count ++;
            }
            Star.prototype.draw=function(){
                var x=Math.sin(this.rand+1)* this.pos+this.dx;
                    y=Math.cos(this.rand)*this.pos/2+this.dy;
                ctx.drawImage(canvas2,x-this.r/2,y-this.r/2,this.r,this.r);
                this.rand=this.rand+this.speed;
            }
            for(var i=0;i<1000;i++){
                new Star();
            }
            function anmit(){
                ctx.clearRect(0,0,w,h);
                for(var i=0;i<stars.length;i++){
                    stars[i].draw();
                }
                requestAnimationFrame(anmit);
            }
            anmit();
            var oH=document.getElementsByTagName("h1")[0];
            var arr=["听我说谢谢你","因为有你,温暖了四季","谢谢你,感谢有你","世界更美丽"],
                index=0,
                arrLen=arr.length,
                strLen=arr[0].length;
                pos=0,
                row=0,
                str="",
                timer=null;
            function print() {
                while(row<index){
                    str=str+arr[row]+"<br>";
                    row++;
                }
                oH.innerHTML=str+arr[index].substring(0,pos);
                if(pos==strLen){
                    index++;
                    pos =0;
                    if(index<arr.length){
                        strLen=arr[index].length;
                        timer=setTimeout(print,250);
                    }
                }else{
                    pos++;
                    timer=setTimeout(print,250);
                }
            }
            setTimeout(print,250);
    </script>
</body>
</html>

配置

 在完成此功能前,需要先确保服务器的配置,能否运行

参考:eclipse使用jsp_无忧#的博客-CSDN博客_eclipse jsp

注意需要配置tomcat和导入servlet-api-jar包

Tomcat服务器下载、安装、配置环境变量教程(超详细)_第三女神程忆难的博客-CSDN博客_tomcat下载

servlet-api-jar包快速下载链接https://how2j.cn/frontdownload?bean.id=270

java 怎么导入jar包_Eclipse怎么导入jar包 Eclipse导入jar包快捷键及图文详细教程_42 legendary的博客-CSDN博客

将java代码放到java包下,将jsp代码和html代码放到webapp包下。

运行Getjsp.jsp。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是使用Java进行HTTP Form提交的示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class HttpFormSubmit { public static void main(String[] args) throws Exception { // 设置URL和请求参数 String url = "http://www.example.com/submitform.php"; Map<String, String> parameters = new HashMap<>(); parameters.put("name", "John Doe"); parameters.put("email", "johndoe@example.com"); parameters.put("message", "Hello, World!"); // 创建连接和请求 URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); // 设置请求参数 DataOutputStream wr = new DataOutputStream(con.getOutputStream()); StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : parameters.entrySet()) { if (sb.length() > 0) { sb.append("&"); } sb.append(URLEncoder.encode(entry.getKey(), "UTF-8")); sb.append("="); sb.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } wr.writeBytes(sb.toString()); wr.flush(); wr.close(); // 获取响应 BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 打印响应 System.out.println(response.toString()); } } ``` 这个示例代码演示了如何使用Java进行HTTP Form提交。它首先设置URL和请求参数,然后创建连接和请求,并将请求参数写入请求体中。最后,它获取响应并打印响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无忧#

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值