标题:ajax servlet的init初始化问题
取消只看楼主
ai5173609
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-4-23
 问题点数:0 回复次数:0 
ajax servlet的init初始化问题
用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号,然后输出记录!
搜索更多相关主题的帖子: ajax servlet init 数据库 服务器 
2007-04-23 09:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-134139-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.224111 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved