diff --git a/jweb/src/edu/hzu/javaweb/labs/se1414080902235/ErrorFilter.java b/jweb/src/edu/hzu/javaweb/labs/se1414080902235/ErrorFilter.java new file mode 100644 index 00000000..549b07ab --- /dev/null +++ b/jweb/src/edu/hzu/javaweb/labs/se1414080902235/ErrorFilter.java @@ -0,0 +1,57 @@ +package edu.hzu.javaweb.labs.se1414080902235servlet; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +/** + * Servlet Filter implementation class LoginFilter + */ +@WebFilter(filterName="ErrorFilter",urlPatterns="/1414080902235") +public class ErrorFilter implements Filter { + + /** + * Default constructor. + */ + public ErrorFilter() { + // TODO Auto-generated constructor stub + } + + /** + * @see Filter#destroy() + */ + public void destroy() { + // TODO Auto-generated method stub + } + + /** + * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) + */ + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + //获取http请求相关数据 + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse resp = (HttpServletResponse) response; + + HttpSession session = req.getSession(); + if (session.getAttribute("username") == null) { + resp.sendRedirect("login.jsp"); + } else { + chain.doFilter(request, response); + } + } + + /** + * @see Filter#init(FilterConfig) + */ + public void init(FilterConfig fConfig) throws ServletException { + System.out.println("登录过滤..."); + } +} diff --git a/jweb/src/edu/hzu/javaweb/labs/se1414080902235/indexcheck.java b/jweb/src/edu/hzu/javaweb/labs/se1414080902235/indexcheck.java new file mode 100644 index 00000000..4fbc75a0 --- /dev/null +++ b/jweb/src/edu/hzu/javaweb/labs/se1414080902235/indexcheck.java @@ -0,0 +1,94 @@ +package web; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class indexcheck extends HttpServlet { + + /** + * Constructor of the object. + */ + public indexcheck() { + super(); + } + + /** + * Destruction of the servlet.
+ */ + public void destroy() { + super.destroy(); // Just puts "destroy" string in log + // Put your code here + } + + /** + * The doGet method of the servlet.
+ * + * This method is called when a form has its tag value method equals to get. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println(""); + out.println(""); + out.println(" A Servlet"); + out.println(" "); + out.print(" This is "); + out.print(this.getClass()); + out.println(", using the GET method"); + out.println(" "); + out.println(""); + out.flush(); + out.close(); + } + + /** + * The doPost method of the servlet.
+ * + * This method is called when a form has its tag value method equals to post. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + String userName=request.getParameter("username"); + String userPwd=request.getParameter("userpwd"); + request.setAttribute("username", userName); + request.setAttribute("userpwd", userPwd); + if("1414080902235".equals(userName)&&"1414080902235".equals(userPwd)) + { + + } + else + { + request.getRequestDispatcher("/show.jsp").forward(request, response); + } + + + + } + + /** + * Initialization of the servlet.
+ * + * @throws ServletException if an error occurs + */ + public void init() throws ServletException { + // Put your code here + } + +} diff --git a/jweb/src/edu/hzu/javaweb/labs/se1414080902235/registerservlet.java b/jweb/src/edu/hzu/javaweb/labs/se1414080902235/registerservlet.java new file mode 100644 index 00000000..998a3780 --- /dev/null +++ b/jweb/src/edu/hzu/javaweb/labs/se1414080902235/registerservlet.java @@ -0,0 +1,84 @@ +package web; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class registerservlet extends HttpServlet { + + /** + * Constructor of the object. + */ + public registerservlet() { + super(); + } + + /** + * Destruction of the servlet.
+ */ + public void destroy() { + super.destroy(); // Just puts "destroy" string in log + // Put your code here + } + + /** + * The doGet method of the servlet.
+ * + * This method is called when a form has its tag value method equals to get. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + } + + /** + * The doPost method of the servlet.
+ * + * This method is called when a form has its tag value method equals to post. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + String userName=request.getParameter("username"); + String userPwd=request.getParameter("userpwd"); + String userPwd1=request.getParameter("userpwd1"); + String userSex=request.getParameter("usersex"); + request.setAttribute("usern",userName); + request.setAttribute("userp",userPwd); + request.setAttribute("users",userSex); + request.setAttribute("power", "0"); + if(userName.length()<=0) + request.getRequestDispatcher("/error.jsp").forward(request, response); + else if(userName.length()<6||userPwd.length()<6) + request.getRequestDispatcher("/error.jsp").forward(request, response); + else if(!(userPwd).equals(userPwd1)) + request.getRequestDispatcher("/error.jsp").forward(request, response); + else + { + request.setAttribute("power", "1"); + request.getRequestDispatcher("/input.jsp").forward(request, response); + } + } + + /** + * Initialization of the servlet.
+ * + * @throws ServletException if an error occurs + */ + public void init() throws ServletException { + // Put your code here + } + +} diff --git a/jweb/web/error.jsp b/jweb/web/error.jsp new file mode 100644 index 00000000..7f0cd6fd --- /dev/null +++ b/jweb/web/error.jsp @@ -0,0 +1,35 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'error.jsp' starting page + + + + + + + + + + + + <% + String username=(String)request.getAttribute("username"); + String userpwd=(String)request.getAttribute("userpwd"); + out.println(username+userpwd); + %>
+鍙戠敓閿欒锛岃妫鏌ョ敤鎴峰悕涓庡瘑鐮佹槸鍚︽纭紒锛侊紒
+ 鐐瑰嚮杩斿洖娉ㄥ唽椤甸潰
+ 鐐瑰嚮杩斿洖鐧婚檰椤甸潰 + + diff --git a/jweb/web/index.jsp b/jweb/web/index.jsp new file mode 100644 index 00000000..f5bc4f67 --- /dev/null +++ b/jweb/web/index.jsp @@ -0,0 +1,54 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ page import="java.io.*,java.util.*" %> +<%@ page import="javax.servlet.*,java.text.*" %> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + 鐧婚檰椤甸潰 + + + + + + + + + +
+ + + + + + + + + + + + +
鐧婚檰
鐢ㄦ埛鍚嶏細*杈撳叆璐﹀彿锛
瀵嗙爜锛*杈撳叆瀵嗙爜锛
+        + +
+
+ 灏氭湭娉ㄥ唽锛熺偣鍑诲墠寰銆 + + <% + Date now=new Date(); + SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); + out.print( "鐜板湪鏃堕棿鏄細"+now + ft.format(now)); + %> + + + diff --git a/jweb/web/info.jsp b/jweb/web/info.jsp new file mode 100644 index 00000000..72c83066 --- /dev/null +++ b/jweb/web/info.jsp @@ -0,0 +1,29 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'info.jsp' starting page + + + + + + + + + + + + <%=request.getAttribute("user") %>
+ <%=request.getAttribute("info") %> + + diff --git a/jweb/web/input.jsp b/jweb/web/input.jsp new file mode 100644 index 00000000..fcb407ce --- /dev/null +++ b/jweb/web/input.jsp @@ -0,0 +1,58 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> +<%@ page import="java.io.*,java.util.*,java.sql.*"%> +<%request.setCharacterEncoding("UTF-8"); %> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'input.jsp' starting page + + + + + + + + + + + + <% + String power=(String)request.getAttribute("power"); + if("1".equals(power)){ + String driveName ="com.mysql.jdbc.Driver"; + String dbName="web"; + String url1="jdbc:mysql://localhost:3306/web"; + String url2="?user=root&password="; + String url3="&useUnicode=true&charsetEncoding=UTF-8"; + String url=url1+url2+url3; + Class.forName("com.mysql.jdbc.Driver"); + Connection conn=DriverManager.getConnection(url); + String sql="Insert into user_information(username,userpwd,sex)values(?,?,?)"; + PreparedStatement pstmt=conn.prepareStatement(sql); + request.setCharacterEncoding("UTF-8"); + String sex=(String)request.getAttribute("users"); + String username=(String)request.getAttribute("usern"); + String userpwd=(String)request.getAttribute("userp"); + pstmt.setString(1,username); + pstmt.setString(2,userpwd); + pstmt.setString(3,sex); + int n=pstmt.executeUpdate(); + if(n==1){%>鏁版嵁鎻掑叆鎿嶄綔鎴愬姛锛<%} + else{%>鏁版嵁鎻掑叆鎿嶄綔澶辫触!<%} + if(pstmt!=null){pstmt.close();} + if(conn!=null){conn.close();} + } + %> +
鐐瑰嚮杩斿洖娉ㄥ唽椤甸潰
+ 鐐瑰嚮鍓嶅線鐧婚檰 + + diff --git a/jweb/web/main.jsp b/jweb/web/main.jsp new file mode 100644 index 00000000..40684865 --- /dev/null +++ b/jweb/web/main.jsp @@ -0,0 +1,38 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> +<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%> + + +涓婚〉闈 + + <% + String power1=(String)session.getAttribute("power1"); + String username=(String)request.getAttribute("username"); + if(power1=="1") + { + out.println("浣犲ソ锛"+username+"(宸茬櫥闄嗐)"); + + Date date=new Date(); + Cookie cookie = null; + Cookie[] cookies = null; + // 鑾峰彇cookies鐨勬暟鎹,鏄竴涓暟缁 + cookies = request.getCookies(); + if( cookies != null ){ + for (int i = 0; i < cookies.length; i++){ + cookie = cookies[i]; + if((cookie.getName()).equals("name")){ + String name = cookie.getValue(); + request.setAttribute("name",name); + break; + } + } + } + + } + else + { + out.println("閿欒锛屼袱绉掑悗杩斿洖鐧婚檰椤甸潰銆傘傘"); + response.setHeader("Refresh","2;url=index.jsp"); + } + %> + + diff --git a/jweb/web/register.jsp b/jweb/web/register.jsp new file mode 100644 index 00000000..ab89590b --- /dev/null +++ b/jweb/web/register.jsp @@ -0,0 +1,36 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> + + + 娉ㄥ唽椤甸潰 + +
+ + + + + + + + + + + + + + + + + + +
娉ㄥ唽椤甸潰
鐢ㄦ埛鍚嶏細*鐢ㄦ埛鍚嶄笉灏忎簬6浣
瀵嗙爜锛*璁剧疆鐧诲綍瀵嗙爜锛岃嚦灏6浣!
纭瀵嗙爜锛*璇峰啀娆¤緭鍏ュ瘑鐮!
鎬у埆锛* + 璇疯緭鍏ヤ綘鐨勬у埆!锛坢ale/female锛
+   + +
+
+ <% + Date now=new Date(); + out.print( "鐜板湪鏃堕棿鏄細"+now ); + %> + + diff --git a/jweb/web/show.jsp b/jweb/web/show.jsp new file mode 100644 index 00000000..3c723819 --- /dev/null +++ b/jweb/web/show.jsp @@ -0,0 +1,44 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> +<%@ page import="java.io.*,java.util.*,java.sql.*"%> +<%request.setCharacterEncoding("UTF-8"); %> + + + + My JSP 'show.jsp' starting page + + + + + <% + String userName=(String)request.getParameter("username"); + String userPwd=(String)request.getParameter("userpwd"); + request.setAttribute("userpwd", userPwd); + request.setAttribute("username",userName); + String dbName="web"; + String url1="jdbc:mysql://localhost:3306/web"; + String url2="?user=root&password="; + String url3="&useUnicode=true&charsetEncoding=UTF-8"; + String url=url1+url2+url3; + Class.forName("com.mysql.jdbc.Driver"); + Connection conn=DriverManager.getConnection(url); + String sql="select * from user_information"; + PreparedStatement pstmt=conn.prepareStatement(sql); + ResultSet rs=pstmt.executeQuery(); + rs.beforeFirst(); + while(rs.next()){ + if(userName==(String)rs.getString(1)&&(String)userPwd==rs.getString(2)) + { + session.setAttribute("power1","1"); + response.setHeader("Refresh","2;url=main.jsp"); + } + + } + if(rs!=null){rs.close();} + if(pstmt!=null){pstmt.close();} + if(conn!=null){conn.close();} + request.getRequestDispatcher("/error.jsp").forward(request, response); + + + %> + + diff --git a/jweb/web/showall.jsp b/jweb/web/showall.jsp new file mode 100644 index 00000000..3f63a8aa --- /dev/null +++ b/jweb/web/showall.jsp @@ -0,0 +1,49 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> +<%@ page import="java.io.*,java.util.*,java.sql.*"%> +<%request.setCharacterEncoding("UTF-8"); %> + + + + + My JSP 'showall.jsp' starting page + + + +
+ <% + String driveName ="com.mysql.jdbc.Driver"; + String url1="jdbc:mysql://localhost:3306/web"; + String url2="?user=root&password="; + String url3="&useUnicode=true&charsetEncoding=UTF-8"; + String url=url1+url2+url3; + Class.forName("com.mysql.jdbc.Driver"); + Connection conn=DriverManager.getConnection(url); + String sql="select * from user_information"; + PreparedStatement pstmt=conn.prepareStatement(sql); + ResultSet rs=pstmt.executeQuery(); + rs.last(); + %>浣犺鏌ヨ鐨勫鐢熸暟鎹〃涓叡鏈 + <%=rs.getRow() %>浜 + + + + + + <%rs.beforeFirst(); + while(rs.next()){ + %> + + + + + + <%} %> +
璁板綍鏉℃暟璐﹀彿瀵嗙爜鎬у埆
<%=rs.getRow() %><%=rs.getString("username") %><%=rs.getString("userpwd") %><%=rs.getString("sex") %>
+
+ <% + if(rs!=null){rs.close();} + if(pstmt!=null){pstmt.close();} + if(conn!=null){conn.close();} + %> + + diff --git a/labs/1414080902235/register.jsp b/labs/1414080902235/register.jsp new file mode 100644 index 00000000..ab89590b --- /dev/null +++ b/labs/1414080902235/register.jsp @@ -0,0 +1,36 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> + + + 娉ㄥ唽椤甸潰 + +
+ + + + + + + + + + + + + + + + + + +
娉ㄥ唽椤甸潰
鐢ㄦ埛鍚嶏細*鐢ㄦ埛鍚嶄笉灏忎簬6浣
瀵嗙爜锛*璁剧疆鐧诲綍瀵嗙爜锛岃嚦灏6浣!
纭瀵嗙爜锛*璇峰啀娆¤緭鍏ュ瘑鐮!
鎬у埆锛* + 璇疯緭鍏ヤ綘鐨勬у埆!锛坢ale/female锛
+   + +
+
+ <% + Date now=new Date(); + out.print( "鐜板湪鏃堕棿鏄細"+now ); + %> + +