在运行web工程时出现一个问题,将Consumer.jsp页面的form提交给servlet竟然出现下载窗口,这是怎么回事?下面是工程代码,哪里出现问题了,请大虾们指点,web.xml配置是正确的,这里代码就不贴出来了Consumer.jsp
<!--提交页面-->
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body>
<center>
<p style="font-family:楷体;">商品查询</p>
<form method="post" action="ShangpinServlet">
<div style="font-family:楷体;">
<div>商品名称:<input type="text" name="d_NAME" /></div>
<div>商品编号:<input type="text" name="d_NUMBER" /></div>
<div> <input type="submit" name="submit1" value="提交" />
<input type="reset" name="reset1" value="重置" /></div>
</div>
</form>
</center>
</body>
</html>Show.jsp
<!--显示页面-->
<%@ page contentType="texy/html;charset=GB2312" %>
<%@ page import="cmx.CommentItem" %>
<%@ page import="java.util.*" %>
<%@ page import="java.lang.*" %> <html>
<body>
<!--<jsp:useBean id="VectorCommand" type="java.util.Vector" scope="request" />-->
<table>
<%
Vector VectorCommand=(Vector)request.getAttribute("VectorCommand");
if(VectorCommand==null){out.print("暂无相关信息");}
if((VectorCommand!=null) && (VectorCommand.size()>0)){
int counter=VectorCommand.size();
CommentItem commentlist=null;
for(int i=0;i<counter;i++){
//commentlist=null;
commentlist=(CommentItem)(VectorCommand.get(i));
%>
<tr>
<td><%=commentlist.getDianMBH() %></td>
<td><%=commentlist.getDianMMC() %></td>
<td><%=commentlist.getDianMJJ() %></td>
<td><%=commentlist.getZuYCP() %></td>
<td><%=commentlist.getDianMWZ() %></td>
<td><%=commentlist.getDianMTEL() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>ShangpinServlet.java
package cmx;import cmx.ShangpinSearch;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class ShangpinServlet extends HttpServlet { /**
* Constructor of the object.
*/
public ShangpinServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String d_NAME=(String)request.getParameter("d_NAME");
String d_NUMBER=(String)request.getParameter("d_NUMBER");
ShangpinSearch command=new ShangpinSearch();
Vector vectorCommand=command.ShangpinDingwei(d_NAME,d_NUMBER);
request.setAttribute("VectorCommand",vectorCommand);
RequestDispatcher dispatcher=request.getRequestDispatcher("Show.jsp");
dispatcher.forward(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}}ShangpinSearch.java
package cmx;import cmx.CommentItem;
import java.sql.*;
import java.util.*;public class ShangpinSearch {
String d_NAME;
String d_NUMBER;
int i_NUMBER=0;
@SuppressWarnings("unchecked")
Vector vectorCommand=new Vector();
CommentItem commentItem=new CommentItem();
Connection con=null;
Statement ps;
ResultSet rs=null; public ShangpinSearch(){}
public Vector ShangpinDingwei(String d_NAME,String d_NUMBER)
{
this.d_NAME=d_NAME;
this.d_NUMBER=d_NUMBER;
String con_user="root";
String con_password="root";
String con_dburl="jdbc:mysql://localhost:3306/test";
String con_driver="com.mysql.jdbc.Driver";
if(!d_NUMBER.equals("")){
i_NUMBER=Integer.parseInt(d_NUMBER);
}
try{
Class.forName(con_driver);
}
catch(Exception e){
}
try{
con=DriverManager.getConnection(con_dburl,con_user,con_password);
String sql ="select * from shangpin where DianMBH=i_NUMBER and DianMMC like '%"+d_NAME+"%'";
ps=con.createStatement();
rs=ps.executeQuery(sql);
while(rs.next()){
commentItem.setDianMBH(rs.getInt(1));
commentItem.setDianMMC(rs.getString(2));
commentItem.setDianMJJ(rs.getString(3));
commentItem.setZuYCP(rs.getString(4));
commentItem.setDianMWZ(rs.getString(5));
commentItem.setDianMTEL(rs.getString(6));
vectorCommand.add(commentItem);
}
vectorCommand.trimToSize();
}
catch(SQLException e){
return null;
}
//request.setAttribute("VectorCommand",vectorCommand);
return vectorCommand;
}
}
CommentItem.java
package cmx;
public class CommentItem {
private int DianMBH;
private String DianMMC;
private String DianMJJ;
private String ZuYCP;
private String DianMWZ;
private String DianMTEL;
public CommentItem(){
DianMBH=0;
DianMMC="";
DianMJJ="";
ZuYCP="";
DianMWZ="";
DianMTEL="";
}
public void setDianMBH(int DianMBH){
this.DianMBH=DianMBH;
}
public int getDianMBH(){
return DianMBH;
}
public void setDianMMC(String DianMMC){
this.DianMMC=DianMMC;
}
public String getDianMMC(){
return DianMMC;
}
public void setDianMJJ(String DianMJJ){
this.DianMJJ=DianMJJ;
}
public String getDianMJJ(){
return DianMJJ;
}
public void setZuYCP(String ZuYCP){
this.ZuYCP=ZuYCP;
}
public String getZuYCP(){
return ZuYCP;
}
public void setDianMWZ(String DianMWZ){
this.DianMWZ=DianMWZ;
}
public String getDianMWZ(){
return DianMWZ;
}
public void setDianMTEL(String DianMTEL){
this.DianMTEL=DianMTEL;
}
public String getDianMTEL(){
return DianMTEL;
}
}
<!--提交页面-->
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body>
<center>
<p style="font-family:楷体;">商品查询</p>
<form method="post" action="ShangpinServlet">
<div style="font-family:楷体;">
<div>商品名称:<input type="text" name="d_NAME" /></div>
<div>商品编号:<input type="text" name="d_NUMBER" /></div>
<div> <input type="submit" name="submit1" value="提交" />
<input type="reset" name="reset1" value="重置" /></div>
</div>
</form>
</center>
</body>
</html>Show.jsp
<!--显示页面-->
<%@ page contentType="texy/html;charset=GB2312" %>
<%@ page import="cmx.CommentItem" %>
<%@ page import="java.util.*" %>
<%@ page import="java.lang.*" %> <html>
<body>
<!--<jsp:useBean id="VectorCommand" type="java.util.Vector" scope="request" />-->
<table>
<%
Vector VectorCommand=(Vector)request.getAttribute("VectorCommand");
if(VectorCommand==null){out.print("暂无相关信息");}
if((VectorCommand!=null) && (VectorCommand.size()>0)){
int counter=VectorCommand.size();
CommentItem commentlist=null;
for(int i=0;i<counter;i++){
//commentlist=null;
commentlist=(CommentItem)(VectorCommand.get(i));
%>
<tr>
<td><%=commentlist.getDianMBH() %></td>
<td><%=commentlist.getDianMMC() %></td>
<td><%=commentlist.getDianMJJ() %></td>
<td><%=commentlist.getZuYCP() %></td>
<td><%=commentlist.getDianMWZ() %></td>
<td><%=commentlist.getDianMTEL() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>ShangpinServlet.java
package cmx;import cmx.ShangpinSearch;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class ShangpinServlet extends HttpServlet { /**
* Constructor of the object.
*/
public ShangpinServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String d_NAME=(String)request.getParameter("d_NAME");
String d_NUMBER=(String)request.getParameter("d_NUMBER");
ShangpinSearch command=new ShangpinSearch();
Vector vectorCommand=command.ShangpinDingwei(d_NAME,d_NUMBER);
request.setAttribute("VectorCommand",vectorCommand);
RequestDispatcher dispatcher=request.getRequestDispatcher("Show.jsp");
dispatcher.forward(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}}ShangpinSearch.java
package cmx;import cmx.CommentItem;
import java.sql.*;
import java.util.*;public class ShangpinSearch {
String d_NAME;
String d_NUMBER;
int i_NUMBER=0;
@SuppressWarnings("unchecked")
Vector vectorCommand=new Vector();
CommentItem commentItem=new CommentItem();
Connection con=null;
Statement ps;
ResultSet rs=null; public ShangpinSearch(){}
public Vector ShangpinDingwei(String d_NAME,String d_NUMBER)
{
this.d_NAME=d_NAME;
this.d_NUMBER=d_NUMBER;
String con_user="root";
String con_password="root";
String con_dburl="jdbc:mysql://localhost:3306/test";
String con_driver="com.mysql.jdbc.Driver";
if(!d_NUMBER.equals("")){
i_NUMBER=Integer.parseInt(d_NUMBER);
}
try{
Class.forName(con_driver);
}
catch(Exception e){
}
try{
con=DriverManager.getConnection(con_dburl,con_user,con_password);
String sql ="select * from shangpin where DianMBH=i_NUMBER and DianMMC like '%"+d_NAME+"%'";
ps=con.createStatement();
rs=ps.executeQuery(sql);
while(rs.next()){
commentItem.setDianMBH(rs.getInt(1));
commentItem.setDianMMC(rs.getString(2));
commentItem.setDianMJJ(rs.getString(3));
commentItem.setZuYCP(rs.getString(4));
commentItem.setDianMWZ(rs.getString(5));
commentItem.setDianMTEL(rs.getString(6));
vectorCommand.add(commentItem);
}
vectorCommand.trimToSize();
}
catch(SQLException e){
return null;
}
//request.setAttribute("VectorCommand",vectorCommand);
return vectorCommand;
}
}
CommentItem.java
package cmx;
public class CommentItem {
private int DianMBH;
private String DianMMC;
private String DianMJJ;
private String ZuYCP;
private String DianMWZ;
private String DianMTEL;
public CommentItem(){
DianMBH=0;
DianMMC="";
DianMJJ="";
ZuYCP="";
DianMWZ="";
DianMTEL="";
}
public void setDianMBH(int DianMBH){
this.DianMBH=DianMBH;
}
public int getDianMBH(){
return DianMBH;
}
public void setDianMMC(String DianMMC){
this.DianMMC=DianMMC;
}
public String getDianMMC(){
return DianMMC;
}
public void setDianMJJ(String DianMJJ){
this.DianMJJ=DianMJJ;
}
public String getDianMJJ(){
return DianMJJ;
}
public void setZuYCP(String ZuYCP){
this.ZuYCP=ZuYCP;
}
public String getZuYCP(){
return ZuYCP;
}
public void setDianMWZ(String DianMWZ){
this.DianMWZ=DianMWZ;
}
public String getDianMWZ(){
return DianMWZ;
}
public void setDianMTEL(String DianMTEL){
this.DianMTEL=DianMTEL;
}
public String getDianMTEL(){
return DianMTEL;
}
}
解决方案 »
- Struts问题:<html:form>在框架里面,Action处理完后,想转发到外面来
- ===========讨论大家做简体版与繁体版是怎么样做法?(客户是香港人,他输入到数据库是繁体,我怎么样在简体时使繁体变简体?)===============
- 谷歌浏览器将颠覆微软(转帖)
- JSP 发送HTML格式问题,百分相送
- 一个简单的strugs程序不能跳转页面
- 求助~dos窗口下import引用问题~~~
- 为什么我在数据库里增加了记录,但是在页面里显示的还是原来没有增加时的记录?怎样解决?
- 有关checkbox的处理问题
- 提取数据库里的时间数据和数据库查询问题
- prepareStatement有时无法从数据库获得数据
- 怎样关闭前一页面,window.close()只能关闭当前页面
- jsp中追加入力项
应该是text吧!