当我调用servlet时,它没有进入所需的jsp页面。下面是两个代码
日志中有一个错误说这个,但这不是一个不转发到目的地的理由
java.sql.SQLException: Bad format for number 'bb' in column 4.
使用此代码调用servlet,非常确定url映射
<a href="/Store/DisplayOrders"> Display Orders</a>
package admin;
import data.ConnectionPool;
import data.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author One
*/
public class DisplayOrdersServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList al = null;
ArrayList ordersList = null;
ordersList = new ArrayList();
String query = "select * from orders order by order_id";
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery(query);
while (rs.next()) {
al = new ArrayList();
al.add(rs.getString("order_id"));
al.add(rs.getString("product_quantity"));
al.add(rs.getString("product_name"));
al.add(rs.getDouble("user_name"));
al.add(rs.getDouble("user_address"));
out.println("al :: " + al);
ordersList.add(al);
}
request.setAttribute("ordersList", ordersList);
String url = "/admin/orders.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
jsp页面
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*;"%>
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Orders CRUD</TITLE>
</HEAD>
<BODY>
<br>
<table>
</table>
<br>
<table >
<tr><td></td></tr>
<tr><td ><a href="/Store/admin/#.jsp">#</a></td></tr>
<tr><td></td></tr>
<tr>
<td>order_id</td><td>product_quantity</td><td>product_name</td>
<td>user_name</td><td>user_address</td>
<td>Edit</td><td>Delete</td>
</tr>
<%
int count=0;
List viewList = new ArrayList();
Iterator viewItr;
if(request.getAttribute("ordersList")!=null && request.getAttribute("ordersList")!="")
{
List orderList = (ArrayList)request.getAttribute("ordersList");
Iterator itr = orderList.iterator();
while(itr.hasNext())
{
viewList = (ArrayList)itr.next();
int product_id = Integer.parseInt(viewList.get(0).toString());
viewItr = viewList.iterator();
%>
<tr>
<%
while(viewItr.hasNext())
{
%>
<td><%=viewItr.next()%></td>
<%
}
count++;
%>
<td><input type="button" name="edit" value="Edit" onclick="editRecord(<%=product_id%>);" ></td>
<td><input type="button" name="delete" value="Delete" onclick="deleteRecord(<%=product_id%>);"></td>
</tr>
<%
}
}
if(count==0)
{
%>
<tr><td > </td></tr>
<tr><td>No Record Avaliable</td></tr>
<%
}
%>
<tr><td></td></tr>
</table>
</BODY>
</HTML>
答案 0 :(得分:1)
servlet有一个很好的理由不转发到JSP页面:forward之前的一个语句抛出SQLException,因此跳过其余的指令,并执行catch块。由于转发是在抛出异常的语句之后的指令的一部分(因此由于异常而被跳过),所以没有转发。
异常可能是由
引起的rs.getDouble("user_name");
我怀疑您的用户名为3.452和67.43; - )
答案 1 :(得分:1)
try {
...
al.add(rs.getDouble("user_name")); <-- Exception thrown
al.add(rs.getDouble("user_address"));
...
String url = "/admin/orders.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
} catch (SQLException e) { <-- Exception catch
e.printStackTrace();
当您收到异常时,将跳过异常后的代码,直到捕获到异常。根本不执行重定向代码。也就是说,将名称或地址作为双精灵是不寻常的。