request常用方法,登录案例
Posted cyxy31521
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了request常用方法,登录案例相关的知识,希望对你有一定的参考价值。
request常用api
1,请求行的api案例代码
@WebServlet(name = "HangServlet",urlPatterns = "/hangServlet") public class HangServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getMethod(); System.out.println("method = " + method); StringBuffer requestURL = request.getRequestURL(); System.out.println("requestURL = " + requestURL); String requestURI = request.getRequestURI(); System.out.println("requestURI = " + requestURI); String remoteAddr = request.getRemoteAddr(); System.out.println("remoteAddr = " + remoteAddr); String protocol = request.getProtocol(); System.out.println("protocol = " + protocol); String contextPath = request.getContextPath(); System.out.println("contextPath = " + contextPath); String queryString = request.getQueryString(); System.out.println("queryString = " + queryString); } }
2,请求头的常用代码案例
@WebServlet(name = "TouServlet", urlPatterns = "/touServlet") public class TouServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getHeader("host"); System.out.println("userName = " + userName); Enumeration<String> headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()){ System.out.println("headerNames.nextElement() = " + headerNames.nextElement()); } } }
3,请求体的代码案例
@WebServlet(name = "TiServlet",urlPatterns="/tiServlet") public class TiServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); System.out.println("username = " + username); String[] hobbies = request.getParameterValues("hobby"); System.out.println("Arrays. = " + Arrays.toString(hobbies)); } }
2,请求转发
特点:
1,跳转的时候,路径不会发生变化,只有一次请求
2,request跳转发生在服务器内部的
3,转发过程中,可以携带数据的
案例代码
跳转到静态页面
@WebServlet(name = "ForwardServlet", urlPatterns="/forward1") public class ForwardServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("name","张三"); request.getRequestDispatcher("/forward2").forward(request,response); }
}
@WebServlet(name = "ForwardServlet2", urlPatterns="/forward2") public class ForwardServlet2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Object name = request.getAttribute("name"); System.out.println("name = " + name); } }
跳转到其他index.html页面:
@WebServlet(name = "ForwardServlet3", urlPatterns="/forward3") public class ForwardServlet3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/login.html").forward(request,response); } }
3、登录案例
1,搭建数据库环境
新建数据库,表
c3p0-config.xml
测试数据库环境
2,静态页面资源web文件
测试页面效果
3,导入jar(WEB-)
4,新建包
5,码代码,测试整个流程
代码实现:
1,数据库配置
2,导入jar包
3,配置工具类
public class JdbcTemplateUtil { private static ComboPooledDataSource cpds = null; private static JdbcTemplate jdbcTemplate = null; static { cpds = new ComboPooledDataSource(); } public static JdbcTemplate getJdbcTemplate(){ jdbcTemplate = new JdbcTemplate(cpds); return jdbcTemplate; } }
4,c3p0-config.xml配置信息
<c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/demo_login</property> <property name="user">root</property> <property name="password">123</property> </default-config> </c3p0-config>
5,dao层代码
public class LoginDao { public User getUser(User user){ JdbcTemplate jdbcTemplate = JdbcTemplateUtil.getJdbcTemplate(); String sql = "select * from login where username=? and password=?"; Object[] args = {user.getUsername(),user.getPassword()}; List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), args); if(list.isEmpty()){ return null; } return list.get(0); } }
6,servlet代码
@WebServlet(name = "LoginServlet", urlPatterns="/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String userName = request.getParameter("userName"); String password = request.getParameter("password"); LoginDao dao = new LoginDao(); User testUser = new User(); testUser.setUsername(userName); testUser.setPassword(password); User user = dao.getUser(testUser); if(user != null){ request.getRequestDispatcher("success.html").forward(request,response); }else{ request.getRequestDispatcher("error.html").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
7,login.html代码
<div> <form action="/loginServlet" method="post"> <h2 >登录页面</h2> <input type="text" name="userName" placeholder="用户名" > <input type="password" name="password" placeholder="密码"> <button type="submit">登录</button> </form> </div>
以上是关于request常用方法,登录案例的主要内容,如果未能解决你的问题,请参考以下文章