Skip to content

Commit

Permalink
Merge pull request #2 from hzuapps/master
Browse files Browse the repository at this point in the history
1111
  • Loading branch information
林烁圭 authored Nov 3, 2016
2 parents 5b09f18 + b4fec09 commit 8293497
Show file tree
Hide file tree
Showing 210 changed files with 11,160 additions and 128 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
https://github.com/hzuapps/java-web/issues/2
# 实验二:Tomcat基础
https://github.com/hzuapps/java-web/issues/3
# 实验三:Servlet基础
https://github.com/hzuapps/java-web/issues/4
# 实验四:Filter基础
https://github.com/hzuapps/java-web/issues/5

# 参考资料

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package edu.hzu.javaweb.labs.se1406070302213;

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 Se1406070302213Servlet extends HttpServlet {

/**
* Constructor of the object.
*/
public Se1406070302213Servlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" Hello!Mr Zeng!");
out.println(" <br>");
out.println(" My student number is 1406070302213.Thanks!");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* 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 {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" Hello!Mr Zeng!");
out.println(" <br>");
out.println(" My student number is 1406070302213.Thanks!");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package edu.hzu.javaweb.labs.se1414080902108;

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;

/**
* Servlet implementation class Se1414080902108Servlet
*/
@WebServlet("/1414080902108")
public class Se1414080902108Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Se1414080902108Servlet() {
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
String subject = request.getParameter("subject");
String question = request.getParameter("question");
String answer1 = request.getParameter("answer1");
String answer2 = request.getParameter("answer2");
String answer3 = request.getParameter("answer3");
String answer4 = request.getParameter("answer4");
String answer = request.getParameter("answer");
String info = "保存成功";


request.setAttribute("Message", info);
request.setAttribute("subject", subject);
request.setAttribute("question", question);
request.setAttribute("answer1", answer1);
request.setAttribute("answer2", answer2);
request.setAttribute("answer3", answer3);
request.setAttribute("answer4", answer4);
request.setAttribute("answer", answer);
request.getRequestDispatcher("/show.jsp").forward(request, response);
}

}
33 changes: 33 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902110/EncodingFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package edu.hzu.javaweb.labs.se1414080902110;

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;

@WebFilter(filterName = "EncodingFilter", urlPatterns = "/*")
public class EncodingFilter implements Filter {

@Override
public void destroy() {
// TODO Auto-generated method stub
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
28 changes: 28 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902110/JDBCconnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.hzu.javaweb.labs.se1414080902110;

import java.sql.*;

public class JDBCconnection {
public Connection connection() {
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userpwd = "123456";
String dbName = "musicforum";
String url1 = "jdbc:mysql://localhost:3306/" + dbName;
String url2 = "?user=" + userName + "&password=" + userpwd;
String url3 = "&useUnicode=true&characterEncoding=UTF-8";
String url = url1 + url2 + url3;
try {
Class.forName(driverName);
} catch (ClassNotFoundException driver) {
driver.printStackTrace();
}
try {
Connection connection = DriverManager.getConnection(url);
return connection;
} catch (SQLException sql) {
sql.printStackTrace();
}
return null;
}
}
45 changes: 45 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902110/LoginFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package edu.hzu.javaweb.labs.se1414080902110;

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;

@WebFilter(filterName = "LoginFilter", urlPatterns = "/essay/*")
public class LoginFilter implements Filter {

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

HttpSession session = req.getSession();
if (session.getAttribute("loginUserAccount") != null) {
chain.doFilter(request, response);
} else {
res.sendRedirect("../error.jsp");
}
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package edu.hzu.javaweb.labs.se1414080902110;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import edu.hzu.javaweb.labs.se1414080902110.JDBCconnection;

public class Se1414080902110Sevlet extends HttpServlet {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;

public Se1414080902110Sevlet() {
super();
}

public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String account = request.getParameter("account");
String password = request.getParameter("password");
try {
JDBCconnection connnection = new JDBCconnection();
conn = connnection.connection();
String sql = "select user_id from user where user_account =? and user_password =?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, account);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
request.getSession().setAttribute("loginUserAccount", account);
request.getRequestDispatcher("/essay.jsp").forward(request,
response);
} else {
request.getRequestDispatcher("/error.jsp").forward(request,
response);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void init() throws ServletException {
// Put your code here
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.hzu.javaweb.labs.se1414080902113;

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

import javax.servlet.ServletConfig;
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.http.HttpSession;

@WebServlet("/1414080902113")
public class Se1414080902113Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public Se1414080902113Servlet() {
super();
}

public void init(ServletConfig config) throws ServletException {

}

public void destroy() {
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=utf-8");
out.println("<HTML>");
out.println(" <HEAD><TITLE>µÚ1¸öServletʾÀý</TITLE></HEAD>");
out.println("<BODY>");
out.println("²âÊԳɹ¦!");
out.println("</BODY>");
out.println("</HTLM>");
out.flush();
out.close();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}
Loading

0 comments on commit 8293497

Please sign in to comment.