Spring_SSH_JSP

  • 最原始的方式,所有的内容都在jsp页面中编写
  • 引入jar包
  • register.jsp------------->表单提交
  • registerHandle.jsp----->表单参数获取,链接数据库,数据库的增删改查工作
  • success.jsp    fail.jsp-->操作结果展示

1.目录结构

2.jar包

3.register.jsp

<%@ 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>用户注册逻辑</title>
  </head>
  <body>
      <form method="post" action="registerHandle.jsp">
          用户名:<input type="text" name="username">
          密码:<input type="password" name="password">
          确认密码:<input type="passwordConfirm" name="passwordConfirm">
          <input type="submit" value="注册">
      </form>
  </body>
</html>

 

4.registerHandle.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

//获取参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String passwordConfirm = request.getParameter("passwordConfirm");

//数据库操作
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring","root","root");

//检查数据库中用户名是否已经存在
String sqlQuery = "select count(*) from user where username = ?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1,username);
ResultSet rs = psQuery.executeQuery();
rs.next();
int count = rs.getInt(1);

//如果存在就返回注册失败的信息,同时关闭系统资源
if(count > 0){
    response.sendRedirect("fail.jsp");
    psQuery.close();
    conn.close();
}

//如果不存在就往数据库中插入一条记录
String sqlInsert = "insert into user values (null,?,?)";
PreparedStatement psInsert = conn.prepareStatement(sqlInsert);
psInsert.setString(1, username);
psInsert.setString(2,password);
psInsert.executeUpdate();
psInsert.close();
conn.close();
response.sendRedirect("success.jsp");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>用户注册</title>
  </head>
  <body>
  </body>
</html>

 

5.success.jsp

<%@ 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>用户注册逻辑</title>
  </head>
  <body>
      注册成功
  </body>
</html>

 

6.fail.jsp

<%@ 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>用户注册逻辑</title>
  </head>
  <body>
      注册失败
  </body>
</html>

 

posted @ 2016-05-26 13:57  桃源仙居  阅读(139)  评论(0)    收藏  举报