开学测试代码

Posted lixv2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开学测试代码相关的知识,希望对你有一定的参考价值。

课上时候,完成了测试中前边的登录注册的代码,实现了登陆时的各种验证,通过数据库存储账户信息,进行判断

注册界面应用了简单的增删改查,主界面套用老师给的html代码。

课下完成了界面的跳转,但是要求中的表格没有完成,之后还要继续学习。

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.DBUtil;
/**
 * Servlet implementation class dengluServlet
 */
@WebServlet("/dengluServlet")
public class dengluServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dengluServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username=(String)request.getParameter("user");
        String pwd=(String)request.getParameter("pwd");
        String randcode=(String)request.getParameter("randcode");
         String sql="select * from user where user=‘"+username+"‘";//定义一个查询语句
            Connection conn = DBUtil.getConnection();
            Statement state = null;    
            ResultSet rs = null;
            try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
             if(rs.next()) {
                 System.out.println("账号存在");
                 if(!pwd.equals(rs.getObject("pwd"))) {
                     PrintWriter out = response.getWriter();
                    out.print("<script>alert(‘密码错误,请重新输入!‘);window.location.href=‘login.html‘</script>");}
                 else {
                     System.out.println("密码正确");
                     PrintWriter out = response.getWriter();
                        out.print("<script>window.location.href=‘main.html‘</script>");
                     
                 }
                 
             }
             else 
                {
                    System.out.println("账号不存在");
                    PrintWriter out = response.getWriter();
                    out.print("<script>alert(‘账户不存在!‘);window.location.href=‘login.html‘</script>");

                }
             rs.close();

        }catch(SQLException e) {
            e.printStackTrace();
        }    
            finally {

            }
        }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action ="AddServlet" method ="post" onsubmit="check(this)">
<table align="center">
<tr>
<td>
<h2>注册账户</h2>
</td>
</tr>
</table>
<table align="center"   width="300">
  <tr>
       <td >用户姓名</td>
       <td><input type ="text" name ="user"></td>
     </tr>
       <tr>
       <td >密码</td>
       <td><input type ="password" name ="pwd"></td>
     </tr>
       <tr>
       <td >重复密码</td>
       <td><input type ="password" name ="pwd2"></td>
     </tr>
    
     <tr>
       <td >手机号码</td>
       <td><input type ="text" name ="num"></td>
     </tr>
     <tr>
       <td >所属单位</td>
       <td><input type ="text" name ="danwei"></td>
     </tr>
     <tr>
     <td align="center" colspan="2">
     <input type="submit" value="     注册     " />
     </td>
     </tr>
</table>
</form>
</body>
</html>
package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import service.User;

/**
 * Servlet implementation class AddServlet
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8"); 
        response.setContentType("text/html;charset=utf-8");
        doGet(request, response);
        String user1 = request.getParameter("user");
        String pwd = request.getParameter("pwd");
        String pwd2=request.getParameter("pwd2");
        String num=request.getParameter("num");
        if(!pwd.equals(pwd2)) {
            PrintWriter out = response.getWriter();
            out.print("<script>alert(‘两次密码不一致‘);window.location.href=‘zhuce.jsp‘</script>");
        }else if(num.length()!=11) {
            PrintWriter out = response.getWriter();
            out.print("<script>alert(‘电话长度错误‘);window.location.href=‘zhuce.jsp‘</script>");
        }else {
        User user = new User();
        user.setUser(user1);
        user.setPwd(pwd);
        UserDao user2 = new UserDao();
        user2.add(user);
        PrintWriter out = response.getWriter();
        out.print("<script>alert(‘注册成功‘);window.location.href=‘zhuce.jsp‘</script>");
        }
    }

}

 

以上是关于开学测试代码的主要内容,如果未能解决你的问题,请参考以下文章

开学测试代码——需求征集系统

java开学第一周测试代码

Java开学测试

Java开学测试感想

2017/2/24日开学测试整理

java开学第一周测试自我感想