html点击按钮传输值,js实现点击按钮传值 - osc_9ajwkns1的个人空间 - OSCHINA - 中文开源技术交流社区...

本文介绍了两个JavaScript函数newDoc()和quzhi(),它们分别用于跳转页面和发送用户输入的数据到服务器。在quzhi()函数中,获取文本框的值,并通过encodeURI编码后拼接到URL中,然后重定向到新的页面。在服务器端,通过Servlet Test_01接收到参数并打印出来。该过程展示了前端与后端的基本交互流程。
摘要由CSDN通过智能技术生成

page1源码:

function newDoc() {

window.location.assign("hellow.html")

}

function quzhi() {

var text1 = document.getElementById("name").value; //取得文本框的值

var text2 = document.getElementById("password").value;

alert(text1);//网页提示框

alert(text2);

var myurl = "Test_01" + "?" + "pname=" + text1 + "&password=" + text2;

window.location.assign(encodeURI(myurl))

}

function quzhi2() {

var text1 = document.getElementById("name").value; //

var text2 = document.getElementById("password").value;

var myurl = "hellow.jsp" + "?" + "pname=" + text1 + "&password="+ text2;

window.location.assign(encodeURI(myurl))

}

page2源码:

Insert title here

hellow world!

page3源码:

Insert title here

String strName=request.getParameter("pname");

String strPassword=request.getParameter("password");

%>

欢迎您!

您的密码是:

servlet源码: package test;

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;

/**

* Servlet implementation class Test_01

*/

@WebServlet("/Test_01")

public class Test_01 extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public Test_01() {

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

response.getWriter().append("Served at: ").append(request.getContextPath());

String strName=request.getParameter("pname");

String strPassword=request.getParameter("password");

System.out.println(strName);

System.out.println(strPassword);

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

}

运行后图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值