关于srevlet

gwang119 2004-09-22 10:20:04
/*
* Created on 2004-9-14
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package login;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;


/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Login_server extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2132";
private Connection conn=null;
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
HttpSession session=request.getSession(true);
//从session中得到数据库连接
conn=(Connection)session.getAttribute("mpsConn");
//如果没有数据库连接,新建一个连接,并且加如session中
try{
if(conn==null){
String url1="jdbc:odbc:SELLSYSTEM";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url1,"","");
session.setAttribute("mps",conn);
}
}
catch(Exception e){
System.out.println(e);
}
//根据用户输入查找
String sql="select * from user where userName='"+request.getParameter("user")+"'";
try{
Statement stm=conn.createStatement();
ResultSet result=stm.executeQuery(sql);
String tempPassword=new String();
String userId=new String();
while(result.next()){
tempPassword=result.getString("password");
userId=result.getString("userId");
}
//于用户填入的密码作比较
if(tempPassword==request.getParameter("password")){
session.setAttribute("userName",request.getParameter("user"));
session.setAttribute("userId",userId);
gotoPage("../index.jsp",request,response);

}else{
out.println("<html>");
out.println("<body>");
out.println("<head><title></title></head>");
out.println("<p>"+"您输入的帐号或密码错误!</p>");
out.println("<p><a href='../index.jsp'>"+"请返回...</a></p>");
out.println("</body></html>");//这里出现乱码!怎么回事?
}

}catch(Exception e){
System.err.println(e);
}
}

//该方法用于定位新的页面
public void gotoPage(String address,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

RequestDispatcher dispacher = getServletContext().getRequestDispatcher(address);
dispacher.forward(request, response);
}



}
...全文
127 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rosifox 2004-09-22
  • 打赏
  • 举报
回复
String tempPassword=new String();
String userId=new String();
这一段没必要吧,String实例是一种immutable对象,
没有人会创建这种空String的,
全部赋与null就行了.

出现乱码的原因不知道:(
whycloud 2004-09-22
  • 打赏
  • 举报
回复
在 server.xml中加入

<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
rosifox 2004-09-22
  • 打赏
  • 举报
回复
conn=(Connection)session.getAttribute("mpsConn");
session.setAttribute("mps",conn);
:) ??怎么属性名不一样啊?
gwang119 2004-09-22
  • 打赏
  • 举报
回复
自己顶一下!

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧