总结
启动MySQL services.msc
pageContext.request.contextPath
request.getRequestDispatcher.forward()
reponse.sendRedirest()
statement.excuteQuery()
statement.executeUpdate()
printStackTrace()
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'index.jsp' starting page
登录
注册
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'login.jsp' starting page
registerjsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'login.jsp' starting page
User.java
package com.jingtian.javabean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author deizhi
* Class.forName("com.mysql.jdbc.Driver");//忘记导包,MyEclipse通过这个包与MySQL相连
*
* con = DriverManager.getConnection(
* "jdbc:mysql://localhost:3306/user", "root",
* "124343");//数据库的名字,以及MySQL的用户名密码
* stm =con.createStatement();
*
*
* rs = stm.executeQuery("select * from information where username = '"
* + username +
* "'");//用SQL语句操作表单//注意SQL语句中varchar类型需要加单引号,MyEclipse中字符串需要加双引号
* ,MySQL需要加单引号
*
* rs.getString("password");//字段名双引号
*/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean login() {
ResultSet rs = null;
Statement stm = null;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/user", "root", "124343");
stm = con.createStatement();
rs = stm.executeQuery("select * from information where username = '"
+ username + "'");
if (rs.next()) {
String dbPassword = rs.getString("password");
if (password.equals(dbPassword)) {
return true;
} else {
return false;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean register() {
ResultSet rs = null;
Statement stm = null;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost/user", "root", "124343");
stm = con.createStatement();
rs = stm.executeQuery("select * from information where username = '"
+ username + "'");
if (rs.next()) {
System.out.println("你注册的用户已经存在!!");
return false;
} else {
stm.executeUpdate("insert into information values('"
+ username + "','" + password + "');");
System.out.println("注册成功!!");
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public int update() {
ResultSet rs = null;
Statement stm = null;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost/user", "root", "124343");
stm = con.createStatement();
rs = stm.executeQuery("select * from information where username = 'aaa'");
if (rs.next()) {
System.out.println("1");
String dbPassword=rs.getString("password");
System.out.println(dbPassword);
if("123".equals(dbPassword))
{
System.out.println("2");
int key=stm.executeUpdate("update information set password = '00000000' where username='aaa';");
return key;
}
else
{
return 0;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("3");
return 0;
}
}
login.java
package com.jingtian.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jingtian.javabean.User;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
// System.out.println(username);
// System.out.println(password);
User user = new User();
user.setUsername(username);
user.setPassword(password);
boolean key=user.login();
if(key)
{
PrintWriter out = response.getWriter();
out.print("登陆成功");
}else
{
PrintWriter out = response.getWriter();
out.print("用户名或密码错误");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
register.java
package com.jingtian.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jingtian.javabean.User;
public class Register extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
// System.out.println(username);
// System.out.println(password);
User user = new User();
user.setUsername(username);
user.setPassword(password);
boolean key=user.register();
if(key)
{
PrintWriter out = response.getWriter();
out.print("注册成功");
}else
{
PrintWriter out = response.getWriter();
out.print("注册失败");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Update.java
package com.jingtian.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jingtian.javabean.User;
public class Update extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
int key=user.update();
PrintWriter out = response.getWriter();
out.print(key);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
web.xml
Register
com.jingtian.controller.Register
Login
com.jingtian.controller.Login
Update
com.jingtian.controller.Update
Delete
com.jingtian.controller.Delete
BadPeople
com.jingtian.controller.BadPeople
Register
/servlet/Register
Login
/servlet/Login
Update
/servlet/Update
Delete
/servlet/Delete
BadPeople
/servlet/BadPeople
index.jsp