一、 myeclipse的tomcat的使用和驱动的放置
软件版本: 编译软件myeclipse2014 数据库mysql2014 驱动mysql-connector-java-5.1.47
1、myeclipse的tomcat的使用
新建立一个java web 项目,在src下面新建里一个servlet类(名叫register)
选择图最上的左边第一个小图标,然后选择自己的项目名字,点击add,然后选择tomcat7。
然后启动服务器,去自己的本地浏览器访问是否能访问成功。
点击绿色的启动,如果更新java和xml代码需要,关闭然后。到服务器设置哪里去点击更新按钮。
浏览器访问
http://127.0.0.1:8080/model_one/
2、驱动的放置
因为需要连接mysql,我们要用到驱动文件
自取地址:链接:https://pan.baidu.com/s/1efejLBwE40JNKd81tuptVA?pwd=top1
提取码:top1
一般放到
二、前端
在WebRoot文件夹下新建立两个hyml页面
<!DOCTYPE html>
<html>
<head>
<title>Login.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" charset="UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/model_one/login" method="post" >
<label>用户名:<input type="text" name="username"/></label> <br />
<label>密码:<input type="text" name="password"/></label> <br />
<input type="submit" value="提交"/>
</form>
</body>
</html>
name属性是需要被servlet获取判断的。
访问:http://127.0.0.1:8080/model_one/Login.html
三、 设置
设置后端的url
xml
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
或者
@WebServlet(urlPatterns = “/myHttpServlet”)
四、mysql的功能代码
建立一个tool类,把下面的方法放到类里面。
写一个属性user user1 = new user();
1、连接数据库并检测是否有库
要把密码和名字换成自己的
要先运行才会有这个库
public static Connection testMysql() throws ClassNotFoundException, SQLException{
Connection con;
String driver = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3306/Usermanage?createDatabaseIfNotExist=true";
String user="root";
String password = "****";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed()){
System.out.println("连接成功!");
}
return con;
}
2、创建表格的
先运行才有这个表
public void cUserTable() throws ClassNotFoundException, SQLException{
Connection con= tool.testMysql();
String sql = "create table userlogin (id int(10) auto_increment primary key,username varchar(20) UNIQUE NOT NULL,password varchar(20) NOT NULL,address varchar(50) NOT NULL)";
Statement stmt= con.createStatement();
ResultSet rs = con.getMetaData().getTables(null, null,"userlogin", null);
if (rs.next()) {
System.out.println("userlogin已经存在");
}else {
stmt.execute(sql);
}
stmt.close();
con.close();
}
3、注册
public boolean addData(String name,String pwd,String address) throws ClassNotFoundException, SQLException{
Connection con= tool.testMysql();
String sql="insert into userlogin (username,password,address) values(?,?,?)";
PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2,pwd);
pst.setString(3,address);
try {
pst.executeUpdate();
pst.close();
con.close();
return true;
} catch (Exception e) {
// TODO: handle exception
pst.close();
con.close();
}
return false;
}
}
4、登录
public boolean login(String name,String password) throws ClassNotFoundException, SQLException{
Connection con = tool.testMysql();
Statement state = con.createStatement();
String sql = "select * from userlogin where username='"+name+"' ";
ResultSet rs= state.executeQuery(sql);
if(rs!=null){
while(rs.next()){
if(password.equals(rs.getString("password"))){
System.out.println("登录成功");
return true;
}
else{
System.out.print("用户密码错误");
return false;
}
}
}
else{
System.out.println("用户不存在");
return false;
}
state.close();
con.close();
return false;
}
5、javaBean
class user{
private String name;
private String password;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "user [name=" + name + ", password=" + password + ", address="
+ address + "]";
}
}
五、register
先写一个静态的tool类型的属性
private tool mytool = new tool();
改写dopost
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
mytool.user1.setName(request.getParameter("username"));
mytool.user1.setPassword(request.getParameter("pwd"));
mytool.user1.setAddress(request.getParameter("address"));
try {
if(mytool.addData(mytool.user1.getName(), mytool.user1.getPassword(),mytool.user1.getAddress())){
//重定向,如果登录成功就跳转到登录页面
response.sendRedirect("/test1/Login.html");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(mytool.user1);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" 注册失败");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
六、login
属性:private tool mytool = new tool();
dopost
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
mytool.user1.setName(request.getParameter("username"));
mytool.user1.setPassword(request.getParameter("password"));
try {
if(mytool.login(mytool.user1.getName(),mytool.user1.getPassword())){
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" 登录成功 ");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" 登录失败");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
运行
数据库
七、补充
getRequestDispatcher是服务器内部跳转,地址栏信息不变,只能跳转到web应用内的网页。(需要写在doget)
sendRedirect是页面重定向,地址栏信息改变,可以跳转到任意网页。
doget解决乱码
new String(request.getParameter(“username”).getBytes(“ISO-8859-1”),“UTF-8”);
dopost解决乱码
response.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset=utf-8”);
response.setContentType(“text/html;charset=UTF-8”);
前端解决乱码
加