<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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"> --> <style type="text/css"> body { background-color: blue; } form { position: fixed; top: 250px; left: 650px; background-color: green; border-radius: 5px; } div { font-size: 22px; margin-top: 20px; padding-left: 40px; } input { width: 200px; height: 20px; border-radius: 5px; } #tj { width: 80px; height: 30px; margin-left: 140px; border-radius: 5px; font-size: 16px; margin-bottom: 20px; } </style> </head> <body> <form name="form1" method="post" action="control.jsp" > <table> <tr> <td>编号:</td> <td> <input type="text" name="sid" id="userName" ></td> </tr> <tr> <td>用户名:</td> <td> <input type="text" name="uname" id="userName" ></td> </tr> <tr> <td>输入登录密码:</td> <td><input type="password" name="upwd" id="pwd"></td> </tr> <tr> <td colspan="2"><input type="submit" value="注册"></td> </tr> </table> </form> </body> </html>
control.jsp
<%@page import="com.gd.dao.StuDao"%> <%@page import="com.gd.bean.Stu"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% Stu s = new Stu(); int id = Integer.parseInt(request.getParameter("sid")); s.setSid(id); String uname = request.getParameter("uname"); s.setUname(uname); String upwd = request.getParameter("upwd"); s.setUpwd(upwd); StuDao sd=new StuDao(); if(sd.addstu(s)>0){ //跳转注册成功页面 }else{ //错误页面 } %> <% if(uname.equals("scg")&&upwd.equals("123")){ //跳转成功登录页面 request.getRequestDispatcher("success.jsp").forward(request, response); session.setAttribute("uname",uname); } else{ //错误页面 request.getRequestDispatcher("index.jsp").forward(request, response); } %>
success.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> 登录成功<br> </body> </html>
Stu.java
package com.gd.bean;
public class Stu {
private int sid;
private String uname;
private String upwd;
//访问器
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
//构造器
public Stu(int sid, String uname, String upwd) {
super();
this.sid = sid;
this.uname = uname;
this.upwd = upwd;
}
public Stu() {
super();
}
}
package com.gd.dao; import java.sql.DriverManager; import java.sql.PreparedStatement; import com.gd.bean.Stu; import com.sun.corba.se.pept.transport.Connection; public class StuDao { public int addstu(Stu s){ int i=0; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8", "root", "root"); //写SQL语句 String sql="insert into stu values(?,?,?)"; //执行 PreparedStatement ps=((java.sql.Connection) con).prepareStatement(sql); ps.setInt(1, s.getSid()); ps.setString(2, s.getUname()); ps.setString(3, s.getUpwd()); i=ps.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; } public int selectUser(Stu u){ int n=0; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8", "root", "root"); String sql="select uname,upwd from stu where uname=?and upwd=?"; PreparedStatement ps=((java.sql.Connection) con).prepareStatement(sql); ps.setInt(1,u.getSid()); ps.setString(2, u.getUname()); ps.setString(3, u.getUpwd()); n=ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } return n; } }
create table stu(
sid int primary key,
uname varchar(20),
upwd varchar(20)
)
insert into stu values(1,'zs','123');
insert into stu values(2,'ls','456');
insert into stu values(3,'scg','111');
delete from stu;