JavaBean里怎么获取表单值
public ArrayList getGoodsByPage(int pageSize,int pageNow){
ArrayList al=new ArrayList();
try {
ct=new JDBConnection().getCon();
ps=ct.prepareStatement("SELECT * FROM goods where goodsName like '%"+keyword+"%' or goodsType like '%"+keyword+"%' order by goodsId");
rs=ps.executeQuery();
while(rs.next()){
GoodsBean gb=new GoodsBean();
gb.setGoodsId(rs.getString(1));
gb.setGoodsPhoto(rs.getString(2));
gb.setGoodsName(rs.getString(3));
gb.setGoodsPrice(rs.getDouble(4));
gb.setGoodsIntro(rs.getString(5));
gb.setGoodsNum(rs.getInt(6));
gb.setGoodsType(rs.getString(7));
al.add(gb);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
this.close();
}
return al;
}
我要获取keyword值
jsp页面的表单:
<form action="goodslist.jsp">
<table>
<tr>
<td width="20" height="50">
<input type="text" size="70" style="height:20px;"name="keyword"/></td>
<td> <input type="submit" value="搜索"></td>
</tr>
</table>
</form> 展开
<form action="goodslist.jsp">点击搜索按钮之后会将请求提交给goodslist.jsp,
goodslist.jsp应该会调用某个Servlet,比如
DemoServlet extends HttpServlet(HttpServletRequest request, HttpServletResponse response),
DemoServlet中重写doPost()方法,
doPost()方法中用String string = request.getParameter("keyword");
就能获取到表单提交的请求参数keyword的内容了。
Servlet获取到的keyword怎么交给java进行ps=ct.prepareStatement("SELECT * FROM goods where goodsName like '%"+keyword+"%' or goodsType like '%"+keyword+"%' order by goodsId");查询啊
Servlet获取到keyword之后就可以调用service层的查询方法--->service层的查询方法会接着调用dao层的查询方法---->dao层的查询方法之中使用JDBC连接数据库进行查询即可。
你不会是想让我给你敲一遍JDBC的连接查询吧???
先写一个表单index.jsp
<form action="setProperty.jsp">
学号:<input type="text" name="xuehao" value="">
姓名: <input type="text" name="xingming" value="">
<input type="submit" value="提交">
</form>
写setProperty.jsp
<jsp:useBean id="s" class="package.Student" scope="page"/>//初始化一个名为s的对象,来自包package下的一个名为Student的类
<jsp:setProperty name="s" propety="sno" param="xuehao">//为s初始化,并用param这个属性 获取表单学号中用户输入的值。
<jsp:getProperty name="s" property="sno">//输出对象s的sno值,即上步得到的值。
3.Student.java
public class Student{
private String sno="";
private String sname ="";
public String getSno(){
return sno;
}
public void setSno(String sno){
this.sno=sno;
}
}