开学测试代码——需求征集系统
生活随笔
收集整理的這篇文章主要介紹了
开学测试代码——需求征集系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,老師給了我們美工做的模板,讓我們使用這個模板進行編程,我當時覺得,哎呀我的媽呀,這是啥!!后來我把文件導入進來后,發現有的.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 stubrequest.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 stubdoGet(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" οnsubmit="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 stubrequest.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>");}}}
轉載于:https://www.cnblogs.com/zmh-980509/p/10474485.html
總結
以上是生活随笔為你收集整理的开学测试代码——需求征集系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Appium+python自动化8-Ap
- 下一篇: CSS: hover选择器的使用