JSP页面,动态表单的复选框传值问题

2025-03-05 05:22:53
推荐回答(4个)
回答1:

String getParameter(String name) 取得表单中指定名字的表单项值
String[] getParameterValues(String name) 取得表单中指定名字的表单项的所有值

下面附带一个例子:
//index.html


测试Servlet页面





请输入姓名:






//MyServlet.java
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

public MyServlet() {//构造函数
super();
}

public void destroy() {//销毁时将调用的方法
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

//处理以GET方式提交过来的表单数据
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);//直接调用doPost方法
}

//处理以Post方式提交上来的表单数据
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("gb2312");//设置响应采用的编码方式
String name = request.getParameter("name");//从提交上来的表单中取name文本框的值
name = new String(name.getBytes("ISO8859-1"),"gb2312");//将字符编码转换为gb2312
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();//得到整个Web应用的ServletContext对象
int count = 1;//从1开始起计
if (context.getAttribute("count")==null){//如果是第一位访客
context.setAttribute("count",new Integer(count));//设置计数器初始值
}else{//如果不是第一位访客
count = Integer.parseInt(context.getAttribute("count").toString());//取出现有的计数值
count++;//计数加1
context.setAttribute("count",new Integer(count));//更新计数器内容
}
out.println("");
out.println(" A Servlet");
out.println(" ");
out.println(name+", 你好!你是第"+count+"位访客!");//实现简单问好
out.println(" ");
out.println("");
out.flush();
out.close();
}

//本Servlet装载到容器后将自动执行的初始化方法
public void init() throws ServletException {
// Put your code here
}
}

回答2:

A.jsp:
提交到form时将a1,a2的值传过去



这样写相当于是将a1的值以("a1",a1)的map形式存放在request中,a2、a3相同,然后再B.jsp就可以从请求中直接获取了。

B.jsp:
<%
String Ba1 = (String)request.getParameter('a1');
%>
这样就可以取得到前一个页面传过来到值了,同理 a2,a3在A.jsp和B.jsp中写法相同,试试先

回答3:

懂得数据库的增删查改吗?懂得主键吗?知道这些就很容易了,A页面从数据库查询的内容每条记录都有一个主键,把主键作为参数传到B页面,然后B页面拿到后去数据库查到该数据,然后把每一个字段取出来,插入你想插入的表,如果你懂JAVABEAN那就更简单了。

回答4:

getParameter()