Skip to content

Commit

Permalink
Merge pull request #2 from hzuapps/master
Browse files Browse the repository at this point in the history
1212152
  • Loading branch information
choisound authored Nov 3, 2016
2 parents 4d1c827 + b4fec09 commit 33c6d56
Show file tree
Hide file tree
Showing 444 changed files with 59,290 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

# 实验一:Web基础
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
38 changes: 38 additions & 0 deletions jweb/src/edu/hzu/javaweb/bean/UserData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
*
*/
package edu.hzu.javaweb.bean;

/**
* @author zengsn
* @since 8.0
*/
public class UserData {
private String username;
private String email;
private int age;

public void setUsername(String value) {
username = value;
}

public void setEmail(String value) {
email = value;
}

public void setAge(int value) {
age = value;
}

public String getUsername() {
return username;
}

public String getEmail() {
return email;
}

public int getAge() {
return age;
}
}
7 changes: 7 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 这里为实验的Java代码。各人的代码放在各自学号的包下。
*
* @author zengsn
* @since 8.0
*/
package edu.hzu.javaweb.labs;
57 changes: 57 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se12345678/Se12345678Servlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package edu.hzu.javaweb.labs.se12345678;

import java.io.IOException;
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("/1st")
public class Se12345678Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public Se12345678Servlet() {
super();
}

public void init(ServletConfig config) throws ServletException {

}

public void destroy() {
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 已登录
String login = "zhang3";
// Session
HttpSession session = request.getSession(true);
session.setAttribute("username", login); // 登录之后
String username = (String) session.getAttribute("username");
if (username == null) {

}

session.setMaxInactiveInterval(15);

// 返回HTML
StringBuilder html = new StringBuilder();
html.append("<!DOCTYPE html><html><head>");
html.append("<title>First App</title>");
html.append("</head><body>");
html.append("<a id=\"link\" href=\"www.hzu.edu.cn\">惠州学院</a>");
html.append("</body></html>");
response.setCharacterEncoding("utf-8");
response.getWriter().append(html);
}

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

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

/**
* 把 12345678 即为学号。
*
* @author zengsn
* @since 8.0
*/
package edu.hzu.javaweb.labs.se12345678;
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

}

}
Loading

0 comments on commit 33c6d56

Please sign in to comment.