Summary of JSP website development 12 and website development 12

Source: Internet
Author: User

Summary of JSP website development 12 and website development 12

The first two articles have briefly introduced you to the knowledge about the Filter interface. In this article, we will use a small login function to specifically implement the role of the Filter for your convenience. We will introduce how to use Filter to Filter access and how to prevent Chinese garbled characters. There is not much content, so you only need to practice it in a simple way.

1. logon form:

There is no difference from a general form. You can create a Login. jsp as the logon interface and add a form to it.

<Body> <center> <form method = "post" action = "<% = request. getContextPath () %>/servlet/login "enctype =" application/x-www-form-urlencoded "> Name: <input type = "text" name = "name"> password: <input type = "password" name = "pwd"> <input type = "submit" value = "Logon"> <input type = "reset" value = "reset"> </form> </center> </body>

2. select:

Since we are logged on, we must have a select class. Because we only discuss the role of the filter, we will not connect to the database here. If you need to visit my previous blogs, provide a detailed description of database connections. Our select Code:

Public class login extends HttpServlet {/*** Constructor of the object. */public login () {super ();}/*** Destruction of the servlet. <br> */public void destroy () {super. destroy (); // Just puts "destroy" string in log System. out. println ("Destroy select");} public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); // prevents Chinese garbled response. setCharacterEncoding ("UTF-8"); // prevents Chinese garbled String username = request. getParameter ("name"); // obtain the name entered by the user in the form String pwd = request. getParameter ("pwd"); // obtain the password entered by the user in the form if ("Xiaomi ". equals (username) & "admin ". equals (pwd) {// identify HttpSession session = request. getSession (); session. setAttribute ("username", username); response. sendRedirect (request. getContextPath () + "/success. jsp ");} else {response. sendRedirect (request. getContextPath () + "/error. jsp ") ;}}/*** Initialization of the servlet. <br> ** @ throws ServletException if an error occurs */public void init () throws ServletException {System. out. println ("initialize select ");}}

2. success. jsp and error. jsp:

To achieve an excellent user experience, we create two new jsp interfaces to provide feedback on the success or failure of logon.

3. Create a Filter class:

Public class firstFilter implements Filter {FilterConfig config; public void destroy () {} public void doFilter (ServletRequest Request, Response, FilterChain arg2) throws IOException, Response {HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) Response; // prevents Chinese garbled req. setCharacterEncoding ("UTF-8"); res. setCharacterEncoding ("UTF-8"); // Filter sets the character set // req. setCharacterEncoding (config. getInitParameter ("lanager"); // res. setCharacterEncoding (config. getInitParameter ("lanager"); HttpSession session = req. getSession (); String username = (String) session. getAttribute ("username"); // System. out. println (new String (username. getBytes ("ISO-8859-1"), "UTF-8"); // prevents Chinese garbled System. out. println (username); // if (req. getRequestURI (). indexOf ("Index. jsp ")! =-1 | req. getRequestURI (). indexOf ("servlet/login ")! =-1) {// arg2.doFilter (Request, Response); // return; //} String noLoginPath = config. getInitParameter ("noLoginPath"); // obtain the default value if (noLoginPath! = Null) {String [] NoPath = noLoginPath. split (";"); for (int I = 0; I <NoPath. length; I ++) {if (req. getRequestURI (). indexOf (NoPath [I])! =-1) {arg2.doFilter (Request, Response); return ;}} if (username! = Null) {arg2.doFilter (Request, Response); // res. sendRedirect (req. getContextPath () + "/success. jsp ");} else {res. sendRedirect (req. getContextPath () + "/index. jsp ") ;}} public void init (FilterConfig arg0) throws ServletException {config = arg0 ;}}

Here we use the FilterConfig object in the init () method for the first time. What is the specific function of this method? When we configure our Filter in web. xml, the default field can be obtained through it.

4. web. xml configuration:

Here we set the default field for the first time.

  

5. Run the test:

Start our project and enter the project name http: // localhost: 8080/Test/index in the address bar. jsp: Go to the logon page. After Successful Logon, copy the address in the address bar and open a new browser to access the address just copied in the address bar, I will find that we have returned to the login interface, which means our filter has taken effect. Of course, our filter functions are far more than that, and the rest will be explored by ourselves.

6. Chinese questions:

In the above Code, we have introduced the issue of preventing Chinese garbled characters.

Today, our summary of the basic JSP knowledge has come to an end. If you have learned more than 12 blogs, I promise to make a simple dynamic website, of course, it is far from enough to develop a website. You also need to learn about div + css and JavaScript. The so-called masters can lead the door and practice depends on individuals, I wish you better and better on the road of programmers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.