日期:2014-05-19 浏览次数:20726 次
import javax.servlet.http.*; import java.io.*; public class Login extends HttpServlet{ //处理get请求 public void doGet(HttpServletRequest req,HttpServletResponse res) { try{ res.setContentType("text/html;charset=utf-8"); PrintWriter pw=res.getWriter(); //res.setContentType("text/html;charset=UTF-8"); //res.setContentType("text/html;charset=gbk"); pw.println("<html>"); pw.println("<body>"); pw.println("<hl>Login</hl><br>"); pw.println("<form action=loginCK method=post>"); pw.println("User:<input type=text name=username> <br>"); pw.println("Passwd:<input type=password name=password> <br>"); pw.println("<input type=submit value=Login> <br>"); pw.println("</form>"); pw.println("</body>"); pw.println("</html>"); }catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res) { this.doGet(req,res); } }
import javax.servlet.http.*; import java.io.*; import java.sql.*; public class LoginCK extends HttpServlet{ //处理get请求 public void doGet(HttpServletRequest req,HttpServletResponse res) { Connection ct=null; PreparedStatement ps=null; ResultSet rs=null; try{ //res.setContentType("text/html;charset=gbk"); //PrintWriter pw=res.getWriter(); //接收用户名和密码 String u=req.getParameter("username"); String p=req.getParameter("password"); //验证 System.out.println(u); System.out.println(p); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); ct=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Students","sa","lissdy033!"); ps=ct.prepareStatement("select top 1 * from [Students].[dbo].[User] where name='"+u+"'and passwd='"+p+"'"); rs=ps.executeQuery(); if(rs.next()) { System.out.println("Wel"); res.sendRedirect("Wel?user="+u+"&pass="+p); }else { System.out.println("login"); res.sendRedirect("login");//写需要跳转的servlet的那个url } if(rs!=null) { rs.close(); } if(ps!=null) { ps.close(); } if(ct!=null) { ct.close(); } }catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res) { this.doGet(req,res); } }
import javax.servlet.http.*; import java.io.*; public class Wel extends HttpServlet{ //处理get请求 public void doGet(Http