Skip to content

Commit

Permalink
Merge pull request #487 from Darkreunion/master
Browse files Browse the repository at this point in the history
#4 #5 提交实验代码
  • Loading branch information
zengsn authored Nov 17, 2016
2 parents bbba216 + f682261 commit 6d3e5d6
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
32 changes: 32 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902239/EncodingFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Filter;

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;

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");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

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

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

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("username") != null) {//µÇ¼ºó²ÅÄÜ·ÃÎÊ
chain.doFilter(request, response);
} else {
res.sendRedirect("failure.jsp");
}
}

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

}

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

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

/**
* Constructor of the object.
*/
public S1() {
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.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
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;charset=UTF-8");
String username=request.getParameter("name1");
String password=request.getParameter("pass1");
if("abc".equals(username)&&"123".equals(password))
{

response.sendRedirect("main.jsp");
}
else{


response.sendRedirect("MyJsp.jsp");



}

}



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

}

0 comments on commit 6d3e5d6

Please sign in to comment.