Java Web知识,MVC模式

1.数据库连接:

try{
    String uri = "jdbc:mysql://localhost:3306/makefriend";
    String user = "root";
    String password = "zhuang";
    Connection conn = DriverManager.getConnection(uri,user,password);
}catch(SQLException e){
    e.printStackTrace();
}


或是:

String uri="jdbc:mysql://localhost:3306/makefriend?"+"user=root&password=zhuang&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
Connection con=DriverManager.getConnection(uri);
Statement stmt = con.createStatement();
sql = "SELECT * FORM scores";
ResultSet rs = stmt.executeUpdate(sql);

2.设置汉字请求相应的字符编码

request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");

3.利用JavaBean类Register声明bean对象,并将bean对象存入request内置对象中

Register reg = new Register();
request.setAttribute("register",reg);
String logName = request.getParameter("logName").trim();
String password = request.getParameter("password").trim();
reg.setLogName(logName);
reg.setPassword(password);

4.获得servlet创建的bean的引用id是servlet所创建的bean索引关键字


通过getProperty获得内容:


5.转发数据

RequestDispatcher dispatcher = request.getRequestDispatcher("showRegisterMess.jsp")
dispatcher.forward(request,response);

 

你可能感兴趣的:(Java,Web知识)