2.bean
package com.ding.bean; /** * @Description TODO * @Author 丁帅帅 * @Date 21/09/11 18:56 * @Version 1.0 */ public class Add { private int a; private int b; public Add() { } public Add(int a, int b) { this.a = a; this.b = b; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int sum(){ int c,s=0; if(a>b){ c=a; a=b; b=c; } for(int x=a;x<=b;++x){ s+=x; } return s; } @Override public String toString() { return "a=" + a + ", b=" + b +",a+...+b="+sum(); } }
3.controller
package com.ding.controller; import com.ding.bean.Add; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @Description TODO * @Author 丁帅帅 * @Date 21/09/11 18:33 * @Version 1.0 */ public class Chuli extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); String s1=req.getParameter("shuju1"); String s2=req.getParameter("shuju2"); int d1=Integer.parseInt(s1); int d2=Integer.parseInt(s2); Add two =new Add(d1,d2); String sum=two.toString(); req.setAttribute("sum",sum); req.getRequestDispatcher("show.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
4.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>default</servlet-name> <servlet-class>com.ding.controller.Chuli</servlet-class> </servlet> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/chuli</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>input.jsp</welcome-file> </welcome-file-list> </web-app>
5.input.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="chuli" method="post"> 数据1:<input name="shuju1"/><br> 数据2:<input name="shuju2"/><br> <input type="submit" value="提交"/> </form> </body> </html>
6.show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>展示页面</title> </head> <body> <% Object sum = request.getAttribute("sum"); out.print(sum); %> </body> </html>
package com.ding.bean; /** * @Description TODO * @Author 丁帅帅 * @Date 21/09/11 19:52 * @Version 1.0 */ public class Student { private int id; private String username; private String userpassword; public Student(int id, String username, String userpassword) { this.id = id; this.username = username; this.userpassword = userpassword; } public Student() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword; } }
package com.ding.controller; import com.ding.dao.UserDao; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/loginCheck") public class LoginCheck extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uname=req.getParameter("uname"); String pwd=req.getParameter("pwd") ; UserDao ud =new UserDao(); boolean flag=ud.login(uname,pwd); if(flag) { req.setAttribute("name", uname); req.getRequestDispatcher("welcome.jsp").forward(req, resp); } else { req.getRequestDispatcher("login.jsp").forward(req, resp); } } }
package com.ding.dao; import com.ding.utis.JDBCUtils; import sun.security.util.Password; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * @Description TODO * @Author 丁帅帅 * @Date 21/09/11 20:13 * @Version 1.0 */ public class UserDao { private Connection conn=null; private PreparedStatement pstmt=null; private ResultSet rs=null; public boolean login(String name, String password) { try { conn=JDBCUtils.getConnection(); String sql="select *from student where name=? and password=?"; pstmt=conn.prepareStatement(sql); pstmt.setString(1,name); pstmt.setString(2,password); rs=pstmt.executeQuery(); /* * if(rs.next()) { return true; }else { return false; } */ return rs.next(); } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtils.close(rs, pstmt, conn); } return false; } }
package com.ding.utis; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * @Description TODO * @Author 丁帅帅 * @Date 21/09/11 20:08 * @Version 1.0 */ public class JDBCUtils { private static String url; private static String user; private static String password; private static String driver; static { try { Properties pro =new Properties(); ClassLoader classLoader=JDBCUtils.class.getClassLoader(); URL res=classLoader.getResource("jdbc.properties"); String path=res.getPath(); pro.load(new FileReader(path)); url=pro.getProperty("url"); user=pro.getProperty("user"); password=pro.getProperty("password"); driver=pro.getProperty("driver"); Class.forName(driver); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,user,password); } public static void close(Statement stmt,Connection conn) { if(stmt!=null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void close(ResultSet rs, Statement pstmt, Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(pstmt!=null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="loginCheck" method="post"> 姓名:<input type="text" name="uname"><br> 密码:<input type="password" name="pwd"><br> <input type="submit" value="登录"><br> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>欢迎${requestScope.name}登录成功!!!</h1> </body> </html>