用servlet作服务器端代码时,在init函数,先把数据库里的内容取出来放在一个数组中,然后再用request.getParameter(“key")取得参数时,这样可以!可是我就想直接在doGet(HttpServletRequest request, HttpServletResponse response)函数中根据request.getParameter("key")来选择数据库里的内容,放在一个数组中,为什么就没有反应了,代码如下:public class ToolServlet extends HttpServlet {
private Map courses = new HashMap();
String sql="";
String colName = "";
String sDBDriver = "";
String sConnStr = "";
private Connection conn ;
private Statement stmt ;
private ResultSet rs,counts ;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Integer key = Integer.valueOf(request.getParameter("key"));
int i;
i=key.intValue();
conn = null;
stmt = null;
sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
sConnStr = "jdbc:odbc:jdomdb";
try{
Class.forName(sDBDriver); ///加载驱动程序
conn = DriverManager.getConnection(sConnStr); ///建立Connection连接
stmt = conn.createStatement(); ///创建语句对象
rs=stmt.executeQuery("select * from userinfo where id=1");
CourseData aa = new CourseData(rs.getString("username"),rs.getString("sex"));
courses.put(new Integer(i), aa);
}
catch(Exception sqlexception)
{
System.out.println("数据库连接发生异常!");
}
CourseData data = (CourseData) courses.get(key);
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
out.println("<response>");
out.println("<par>" + data.getPar() + "</par>");
out.println("<length>" + data.getString()+ "</length>");
out.println("</response>");
out.close();
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
private class CourseData {
private String par;
private String length;
public CourseData(String par, String length) {
this.par = par;
this.length = length;
}
public String getPar() {
return this.par;
}
public String getString() {
return this.length;
}
}
}
大家帮我看一下!谢谢了,我的代码的意思就是想根据前台KEY参数的值去查询数据库中对应的ID号,然后输出记录!