81,092
社区成员
发帖
与我相关
我的任务
分享
public class Login extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void service(HttpServletRequest req,HttpServletResponse resp) throws IOException{
HttpSession session=req.getSession(true);
PrintWriter out = resp.getWriter();
String username=req.getParameter("username");
String pwd=req.getParameter("pwd");
String sql="select * from userinfo where username=? and pwd=?";
Connection conn=db.getConnection();
try{
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,pwd);
ResultSet rs=pstmt.executeQuery();
Boolean m=rs.next();
if(m==true){
session.setAttribute(username, rs.getString("username"));
session.setAttribute(pwd,rs.getString("pwd"));
resp.sendRedirect("index.jsp");
}else{
out.println("<SCRIPT LANGUAGE=javascript>");
out.println("alert('用户名或密码错误!');");
out.println("window.location.href='default.jsp'; ");
out.println("</script>");
}
pstmt.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
public class gl extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void service(HttpServletRequest req,HttpServletResponse resp) throws IOException{
HttpSession session=req.getSession(true);
PrintWriter out = resp.getWriter();
String username=null;
String lastip=null;
String sql="select username,lastip from log";
Connection conn=db.getConnection();
try{
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery(sql);
Boolean m=rs.next();
if(m==true){
session.setAttribute(username,rs.getString("username"));
session.setAttribute(lastip,rs.getString("lastip"));
System.out.println(username);
}else{
out.println("<SCRIPT LANGUAGE=javascript>");
out.println("alert('用户名或密码错误!');");
out.println("window.location.href='default.jsp'; ");
out.println("</script>");
}
pstmt.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'default.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<form action="login" name="form1" method="post">
<tr>
<td>用户</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td height="45" colspan="2">
<p align="center">
<input type="submit" value="登 陆">
<input type="reset" value="取 消">
</td>
</tr>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="org.gl.*"%>
<%@ page import="java.util.Date"%>
<jsp:useBean id="db" class="org.gl.db" scope="page"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String username=(String)session.getAttribute("username");
String lastip=(String)session.getAttribute("lastip");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>XXXXXXXXXXXXXXXX This is my JSP page. <br>
<form action="gl" method="post">
<table border="1">
<tr><td><%=username%></td><td><%=lastip%></td></tr>
</table>
</form>
</body>
</html>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>org.gl.Login</servlet-class>
</servlet>
<servlet>
<servlet-name>gl</servlet-name>
<servlet-class>org.gl.gl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>gl</servlet-name>
<url-pattern>/gl</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>