记账本开发记录——第十五天(2020.2.2)
今天简单学习了一下request的使用方法,与昨天的response相对应的,requset为HTTP请求。request请求的运行过程与response大致一致,这里就不再赘述。
首先,request也可以获取请求头,请求头和请求体。每个依次写了一个demo:
package com.ithiema.header; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HeaderServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、获得指定的头 String header = request.getHeader("User-Agent"); System.out.println(header); //2、获得所有的头的名称 Enumeration<String> headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()){ String headerName = headerNames.nextElement(); String headerValue = request.getHeader(headerName); System.out.println(headerName+":"+headerValue); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.ithiema.line; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LineServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、获得请求方式 String method = request.getMethod(); System.out.println("method:"+method); //2、获得请求的资源相关的内容 String requestURI = request.getRequestURI(); StringBuffer requestURL = request.getRequestURL(); System.out.println("uri:"+requestURI); System.out.println("url:"+requestURL); //获得web应用的名称 String contextPath = request.getContextPath(); System.out.println("web应用:"+contextPath); //地址后的参数的字符串 String queryString = request.getQueryString(); System.out.println(queryString); //3、获得客户机的信息---获得访问者IP地址 String remoteAddr = request.getRemoteAddr(); System.out.println("IP:"+remoteAddr); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
关于请求头,写了一个注册的小demo,代码如下:
package com.ithiema.register; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.util.Map; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.dbutils.QueryRunner; import com.ithiema.utils.DataSourceUtils; public class RegisterServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置request的编码---只适合post方式 request.setCharacterEncoding("UTF-8"); //get方式乱码解决 //String username = request.getParameter("username");//乱码 //先用iso8859-1编码 在使用utf-8解码 //username = new String(username.getBytes("iso8859-1"),"UTF-8"); //1、获取数据 //String username = request.getParameter("username"); //System.out.println(username); //String password = request.getParameter("password"); //..... //2、将散装的封装到javaBean //User user = new User(); //user.setUsername(username); //user.setPassword(password); //使用BeanUtils进行自动映射封装 //BeanUtils工作原理:将map中的数据 根据key与实体的属性的对应关系封装 //只要key的名字与实体的属性 的名字一样 就自动封装到实体中 Map<String, String[]> properties = request.getParameterMap(); User user = new User(); try { BeanUtils.populate(user, properties); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } //现在这个位置 user对象已经封装好了 //手动封装uid----uuid---随机不重复的字符串32位--java代码生成后是36位 user.setUid(UUID.randomUUID().toString()); //3、将参数传递给一个业务操作方法 try { regist(user); } catch (SQLException e) { e.printStackTrace(); } //4、认为注册成功跳转到登录页面 response.sendRedirect(request.getContextPath()+"/login.jsp"); } //注册的方法 public void regist(User user) throws SQLException{ //操作数据库 QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)"; runner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getName(), user.getEmail(),null,user.getBirthday(),user.getSex(),null,null); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
关于注册的业务逻辑和思路,已经很熟练,不再赘述。主要是其中的BeanUtils,这是一个工具类,通过BeanUtils可以很方便的将内容封存到实体中去。
以上是今天学习的内容
今日的问题:在今天的学习中,发现无法按照老师所说的存放到数据库中后是正常的文字,依然是乱码。为了解决这个问题,从网上各种搜寻无果,最后发现自己连数据库也连不上了。在发这篇博客之前我甚至都不能存储数据,说实话是挺气的,等博客发布后再对其进行一下修复试试,明天再看吧