Experiment 3 first jsp pages copied;
WebContent-> WEB-INF-> lib jar package below version 8.0 have to remember to copy;
Java Resources-> cn.edu.henu.bean (User.java user database operations) in the src and cn.edu.henu.util (DBConnection.java database connection operation) the two packets
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ws_shiyan3</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>addUser.jsp</welcome-file>
The use of Servlet technology to rewrite the user logs
src -> New to create a package "cn.edu.henu.servlet" -> Create a servlet class name "LoginCheckServlet" -> URL mapping is set to "/ CheckServlet"
rewrite dopost () method, the third experiment check.jsp landing authentication code can be copied
String name=request.getParameter("name"); String pwd=request.getParameter("pwd"); PreparedStatement pt=null; Connection con = null; User user = null; try { //JDBC访问数据库 con=DBConnection.openConnection();//获取数据库连接 String sql="SELECT * from login WHERE user=? AND passwod=?"; pt=con.prepareStatement(sql); pt.setString(1, name); pt.setString(2, pwd); ResultSet rs=pt.executeQuery(); System.out.println("?????????????????????"); if(rs.next()) { response.sendRedirect("connsuccess.jsp"); } else{ response.sendRedirect("connError.jsp"); } } catch (SQLException e) { E.printStackTrace ();}
You can also refer to this JSP + Servlet user's login
[Note] here User user = null; below the yellow alarm so I just started commented out, the results CheckServlet on page 404, later canceled before they can comment
There are two ways to configure the Servlet
① automatically @WebServlet ( "/ CheckServlet") LoginCheckServlet page generated
② disposed below web.xml
<the servlet> <the servlet-name> LoginCheckServlet </ the servlet-name> // create an object using the class name <servlet-class> cn.edu.henu.servlet.LoginCheckServlet </ servlet-class> // servlet class created their own </ servlet> <-Mapping the servlet> <the servlet-name> LoginCheckServlet </ servlet-name> is the actual implementation of the object // <url-pattern> / CheckServlet < / url-pattern> // URL Mapping custom </ servlet -mapping>
Note] Oh, here name to match
[Note] two methods can only choose a configuration, otherwise it will error
to use technology to solve the Chinese garbled Filter
src -> new Filter (package name is "cn.edu.henu.Filter", Filter name is "EncodingFilter", MAP did not change my name EncodingFilter
then enter request.setCharacterEncoding ( "utf-8" in (under doFilter) ); to
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here request.setCharacterEncoding("utf-8"); // pass the request along the filter chain chain.doFilter(request, response); }
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>cn.edu.henu.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> //拦截所有请求 </filter-mapping>
Original: https: //blog.csdn.net/wwjlovecyndi/article/details/90300255