Summary of JSP website development 7 and jsp website development 7

Source: Internet
Author: User

Summary of JSP website development 7 and jsp website development 7

According to the plan, this article will summarize the two implementations of the search function: Determining search and fuzzy search. The so-called exact search refers to finding data with a one-to-one correspondence relationship in the database based on the search content entered by the user, which is generally the primary key value of the user in the database. Fuzzy search is a type of matching based on user input, and returns data objects with similar information to the user. The difference between the two lies only in the difference between SQL statements. The following is a detailed introduction.

The design of the search input box has been introduced in the previous article. I will not go into details here. Next we will start the content of this article.

1. Exact search:

Java method used for search:

public Student getName(String name) {        Student student = new Student();        Connection conn = null;        Statement st = null;        ResultSet rs = null;        conn = DBO.getConnection();        String sql = "select * from students where username='"+name+"'";        try {            st = conn.createStatement();            rs = st.executeQuery(sql);            while(rs.next()){                student.setId(rs.getInt("id"));                student.setName(name);                student.setSex(rs.getInt("sex"));                student.setYear(rs.getInt("age"));                student.setFrom(rs.getString("form"));                student.setSchool(rs.getString("school"));            }        } catch (SQLException e) {            e.printStackTrace();        }        return student;    }

2. Fuzzy search:

Java method used for fuzzy search:

public List<Student> getByName(String name) {        List<Student> list = new ArrayList<Student>();        Connection conn = null;        PreparedStatement pst = null;        ResultSet rs = null;        conn = DBO.getConnection();        String sql = "select * from students where username like ?";                try {            pst = conn.prepareStatement(sql);            pst.setString(1, "%"+name+"%");            rs = pst.executeQuery();            while(rs.next()){                Student student = new Student();                student.setId(rs.getInt("id"));                student.setName(rs.getString("username"));                student.setSex(rs.getInt("sex"));                student.setYear(rs.getInt("age"));                student.setFrom(rs.getString("form"));                student.setSchool(rs.getString("school"));                list.add(student);            }        } catch (SQLException e) {            e.printStackTrace();        }        return list;    }

3. select Code:

Here I put the select for exact search and fuzzy search together.

Public class get extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); StudentMaImp smi = new StudentMaImp (); Student student = new Student (); List <Student> list = new ArrayList <Student> (); if (request. getParameter ("queding ")! = Null & request. getParameter ("queding"). length ()! = 0) {String queding = request. getParameter ("queding"); student = smi. getName (queding); if (student. getName ()! = Null & student. getName (). length ()! = 0) {request. setAttribute ("student", student); request. getRequestDispatcher ("Get. jsp "). forward (request, response);} else {list = smi. getAll (); request. setAttribute ("list", list); request. setAttribute ("new1", queding + "student does not exist"); request. getRequestDispatcher ("All. jsp "). forward (request, response) ;}} else {String mohu = request. getParameter ("mohu"); list = smi. getByName (mohu); if (list. size ()! = 0) {request. setAttribute ("list", list); request. getRequestDispatcher ("GetAll. jsp "). forward (request, response);} else {list = smi. getAll (); request. setAttribute ("list", list); request. setAttribute ("new2", "Keyword:" + mohu + "nonexistent"); request. getRequestDispatcher ("All. jsp "). forward (request, response );}}}}

4. Remember the username and password:

We often see on many websites that need to log on. When we log on successfully for the first time and then open this page again, our user name and password have been filled in, this is a password-remembering function. Next I will introduce how to use it. Here we need to use a keyword Cookie. Through this keyword, we can save our account information locally through the select background. When we need to use this keyword, we can call a method of Cookie.

How to save data to a local machine:

Cookie cookie_name = new Cookie ("name", name); // set the name of the stored data cookie_name.setMaxAge (60*60*24*7 ); // set the data retention period to one week's response. addCookie (cookie_name); Cookie cookie_pwd = new Cookie ("pwd", password); cookie_pwd.setMaxAge (60*60*24*7); response. addCookie (cookie_pwd );

Obtain the value in the Cookie:

<Tr> <td> name: </td> <input type = "text" name = "name" value = "$ {cookie. name. value} "/> </td> </tr> <td> password: </td> <input type = "password" name = "pwd" value = "$ {cookie. pwd. value} "/> </td> </tr>

The summary in this article is complete, and there is not much content. If you have any questions, please leave a message for discussion. Next article: effect implementation similar to Baidu Paging

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.