学习day27

学习的内容:


开始步入servlet的学习,把所有的包、驱动、jdbc、DBHelper各种东西都解决后开始设计、编写。


首先学习了简单的登录注册:
登录页面核心代码:
${message}

。。。省略部分代码。。。





在LoginController中的代码:
一般有四步:1、设置字符编码  2、接受请求参数  3、处理参数  4、跳转页面
@WebServlet(value = "/login")
public class LoginController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String massege = "";
        String path = request.getContextPath();
        //1、utf
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //2、请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //3、处理参数 对比
        if("nihaoma".equals(username)&&"666".equals(password)){
//4、返回结果
            response.sendRedirect(path+"/index");
//  request.getRequestDispatcher(path+"/index").forward(request,response);
        }else {
            massege = "显然没输入nihaoma和666";
            request.setAttribute("massege",massege);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }
        
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//在此处还不必用到doPost()方法}




接下来学习的是查询:
在IndexController跳转到index.jsp
<%
    String path = request.getContextPath();
    //通过封装的 list 拿到index中的rs 要把obj转成resultset
    ResultSet rs = (ResultSet)request.getAttribute("list");
    //遍历
    while(rs.next()) {
。。。省略。。。
out.print("删除");
    }


%>




**重定向是get请求,请求转发时是表单提交的一般是post。
可以用doGet(request,response)和doPost(request,response)


最后还学习了删除功能:
在index..jsp界面通过:
out.print("删除");
来跳转到DeleteController.java
还是那四步:
1、设置字符编码  
2、接收请求参数  
3、处理参数(DBHelper、sql、i=helper.executeUpdate(sql,id);、if语句,若抛不了异常还需要手抓异常。)
4、跳转页面:
request.setAttribute("message",message);
        request.getRequestDispatcher("index").forward(request,response);








学习的问题:
自己模仿写了增加功能,添加学生的功能有些ID添加不了,有些ID能成功,很懵。

你可能感兴趣的:(学习day27)