码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-05 09:44:27      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:rate   手机号   href   finally   class   pos   text   long   文件导入   

首先,老师给了我们美工做的模板,让我们使用这个模板进行编程,我当时觉得,哎呀我的妈呀,这是啥!!后来我把文件导入进来后,发现有的.html文件是可以跑起来的,然后我就开始写测试。刚开始进展很顺利,但是后来因为我的一点疏忽,破坏了tomcat的使用,因为程序不能进行测试,耽误了我很多时间。代码没有写完,尽管课下进行了补充,但是还是很菜。

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>");
        }
    }

}

  

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

标签:rate   手机号   href   finally   class   pos   text   long   文件导入   

原文地址:https://www.cnblogs.com/zmh-980509/p/10474485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!