如何在.java文件中用request获得jsp页面中单选按钮传来的值?

zzzsn 2014-05-20 08:28:10
菜鸟一只!!!使用struts2,jdbc写投票程序
写vote.jsp
关键部分:
<%
Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/zsn;user=sa;password=zsn";
Connection DBconn=java.sql.DriverManager.getConnection(dbUrl);
Statement stmt = DBconn.createStatement();
ResultSet rs=stmt.executeQuery("select * from vote2");
while(rs.next())
{
out.println("<tr>");
out.println("<td><input type='radio' name='id' value='"+rs.getString("id")+"'>");
out.println(rs.getString("item"));
out.println("</td>");
}
rs.close();
stmt.close();
session.setMaxInactiveInterval(-1);
%>
<s:form action="vote" method="post">
<tr><td align="center">
<s:submit value="投票" theme="simple"></s:submit>


struts.xml中:
<action name="vote2" class="laogen.action.vote2">
<result name="OK">/success.jsp</result>


vote2.java如下:
package laogen.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import laogen.common.DBcon;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class vote2 extends ActionSupport{
public String execute() throws Exception {
Connection conn = DBcon.getConnection();// 这是用来连通数据库
Statement stmt = null;

try {

String id=request.getParameter("id");

ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id);
int num=0;
if(rs.next())
num=rs.getInt("count");
num++;
rs.close();
stmt.executeUpdate("update vote set count="+num+" where id="+id);
stmt.close();

} catch (Exception e) {
e.printStackTrace();
}
return "OK";
}
}

request报错,request cannot be resolved。
要想用request直接获取.jsp文件中的值,改怎么办??


...全文
1011 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
什么都不能 2014-05-22
  • 打赏
  • 举报
回复
<s:form action="vote" method="post"><% 
Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
String dbUrl = "jdbc:jtds:sqlserver://localhost:1433/zsn;user=sa;password=zsn";
Connection DBconn=java.sql.DriverManager.getConnection(dbUrl);
Statement stmt = DBconn.createStatement();
ResultSet rs=stmt.executeQuery("select * from vote2"); 
 while(rs.next())  
{   
out.println("<tr>");   
out.println("<td><input type='radio' name='id' value='"+rs.getString("id")+"'>"); 
  	out.println(rs.getString("item"));   
out.println("</td>");  
}  
rs.close();  
stmt.close();  
session.setMaxInactiveInterval(-1);
 %> 

 <tr><td align="center">
  <s:submit value="投票" theme="simple"></s:submit>
</tr>
</s:form>
struts.xml中: <action name="vote2" class="laogen.action.vote2"> <result name="OK">/success.jsp</result>
zzzsn 2014-05-22
  • 打赏
  • 举报
回复
引用 13 楼 sssmmmhhh 的回复:
1.如果使用struts2,你的jsp页面上需要传递的数据的文本框,定义其name属性,相应的后台action,只需要定义这个变量,拥有其get/set方法,就可以了,我看你还是用post提交那就更好了了。既然使用struts2就得好好利用。2,。utl传值, String id=request.getParameter("id");
层主说的好!!我的还有一个错误就在这里!!求指点!! 这是vote2.java中的try try { HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+ id ); int num=0; if(rs.next()) num=rs.getInt("count"); num++; System.out.println(num); rs.close(); conn.setAutoCommit(false); stmt.executeUpdate("update vote2 set count="+ num +" where id=" + id ); stmt.close(); conn.commit(); } 这个id是从.jsp文件中获取的 这是.jsp文件中的相关部分 ResultSet rs=stmt.executeQuery("select * from vote2"); while(rs.next()) { out.println("<tr>"); out.println("<td><input type='radio' name='id' value='"+rs.getString("id")+"'>"); out.println(rs.getString("item")); out.println("</td>"); } rs.close(); stmt.close(); session.setMaxInactiveInterval(-1); 我定义的每一个单选按钮对应一个name,name=“id”,在.java文件中用 String id=request.getParameter("id"); ,想获取jsp页面中的id的值,来对i相应id对应的投票数目进行更新,stmt.executeUpdate("update vote2 set count="+ num +" where id=" + id ); 但是数据库一直没有改变,如果把id直接换成对应的数字,如id='1',就可以更新,这是为什么??
zzzsn 2014-05-22
  • 打赏
  • 举报
回复
引用 8 楼 jiji123go 的回复:
[quote=引用 7 楼 jiji123go 的回复:] Connection conn = DBcon.getConnection();// 这是用来连通数据库 Statement stmt = null; try { HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); 你stmt都没初始化。。。。。
Statement stmt = null; ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); stmt没实例化,肯定空指针异常[/quote] 确实确实。。。加上了stmt = conn.createStatement();
zzzsn 2014-05-22
  • 打赏
  • 举报
回复
引用 10 楼 u013972119 的回复:
stmt.close(); stmt = conn.createStatement(); stmt.executeUpdate("update vote set count="+ num +" where id="+id);
谢谢~~上面的问题已经解决,加上了 conn.setAutoCommit(false); 中间是更新的代码; conn.commit();也行~
楠楠-smh 2014-05-22
  • 打赏
  • 举报
回复
1.如果使用struts2,你的jsp页面上需要传递的数据的文本框,定义其name属性,相应的后台action,只需要定义这个变量,拥有其get/set方法,就可以了,我看你还是用post提交那就更好了了。既然使用struts2就得好好利用。2,。utl传值, String id=request.getParameter("id");
T114648214277 2014-05-21
  • 打赏
  • 举报
回复
来学习下!
  • 打赏
  • 举报
回复
来学习下!
Mr_sqw 2014-05-20
  • 打赏
  • 举报
回复
stmt.close(); stmt = conn.createStatement(); stmt.executeUpdate("update vote set count="+ num +" where id="+id);
Mr_sqw 2014-05-20
  • 打赏
  • 举报
回复
引用 2 楼 zzzsn 的回复:
[quote=引用 1 楼 u013972119 的回复:] HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id");
谢谢!!这样确实不报错了,不过投票之后数据库的内容并没有改变,我感觉我写的update没有错,放在sql中执行也可以,为什么就是不能更新呢?? ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); stmt.close(); } catch (Exception e) { e.printStackTrace(); } return "OK";[/quote] 参考8楼
jiji123go 2014-05-20
  • 打赏
  • 举报
回复
引用 7 楼 jiji123go 的回复:
Connection conn = DBcon.getConnection();// 这是用来连通数据库 Statement stmt = null; try { HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); 你stmt都没初始化。。。。。
Statement stmt = null; ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); stmt没实例化,肯定空指针异常
jiji123go 2014-05-20
  • 打赏
  • 举报
回复
Connection conn = DBcon.getConnection();// 这是用来连通数据库 Statement stmt = null; try { HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); 你stmt都没初始化。。。。。
zzzsn 2014-05-20
  • 打赏
  • 举报
回复
引用 4 楼 jiji123go 的回复:
看下是否将Connection 的AutoCommit设置为false了 如果是,事物需要显示提交conn.commit();
没有设置过AutoCommit,默认是false吗?这个在sql中怎么修改?
zzzsn 2014-05-20
  • 打赏
  • 举报
回复

这是表vote2.
jiji123go 2014-05-20
  • 打赏
  • 举报
回复
看下是否将Connection 的AutoCommit设置为false了 如果是,事物需要显示提交conn.commit();
zzzsn 2014-05-20
  • 打赏
  • 举报
回复
vote2.java: package laogen.action; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import laogen.common.DBcon; //DBcon类用来连接数据库,可以连接 import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class vote2 extends ActionSupport{ public String execute() throws Exception { Connection conn = DBcon.getConnection();// 这是用来连通数据库 Statement stmt = null; try { HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id"); ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); stmt.close(); } catch (Exception e) { e.printStackTrace(); } return "OK"; } }
zzzsn 2014-05-20
  • 打赏
  • 举报
回复
引用 1 楼 u013972119 的回复:
HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id");
谢谢!!这样确实不报错了,不过投票之后数据库的内容并没有改变,我感觉我写的update没有错,放在sql中执行也可以,为什么就是不能更新呢?? ResultSet rs=stmt.executeQuery("select * from vote2 where id="+id); int num=0; if(rs.next()) num=rs.getInt("count"); num++; rs.close(); stmt.executeUpdate("update vote set count="+ num +" where id="+id); stmt.close(); } catch (Exception e) { e.printStackTrace(); } return "OK";
Mr_sqw 2014-05-20
  • 打赏
  • 举报
回复
HttpServletRequest request = ServletActionContext.getRequest(); String id=request.getParameter("id");

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧