上传文件
-
servlet文件代码
public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先判度上传的数据是否为多段数据,(只有多段数据才 // 能上传) if (ServletFileUpload.isMultipartContent(req)){ FileItemFactory fileItemFactory = new DiskFileItemFactory(); //创建用于解析上传数据的工具类ServletFileUpload ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory); try { List<FileItem> list = servletFileUpload.parseRequest(req); for (FileItem fileItem : list){ if (fileItem.isFormField()){ //普通表单项 System.out.println("表单项的username值"+fileItem.getFieldName()); System.out.println("表单项的values值"+fileItem.getString("UTF-8")); }else{ System.out.println("上传文件的name"+fileItem.getFieldName()); System.out.println("上传文件名"+fileItem.getName()); fileItem.write(new File("C:/")); } } } catch (Exception e) { e.printStackTrace(); } } } }
-
html代码
<body> <form action="LoginServlet" enctype="multipart/form-data" method="post"> 用户名:<input type="text" name="username"><br> 头像 <input type="file" name="photo" ><br> <input type="submit" value="上传"> </form> </body> </html>
-
在用户输入错误的账号或者密码时回显账号,并且提示错误
LoginServlet文件代码
//如果密码或者用户错误,回显信息 req.setAttribute("msg","你输入的密码错误"); req.setAttribute("username",username); //用来回显账户
login.jsp文件代码
```jsp <%=request.getAttribute("msg")==null? "请输入用户名和密码":request.getAttribute("msg")%> //其中,用户回显,在用户的value中写 <label>用户名称:</label> <input class="itxt" type="text" placeholder="请输入用户名" autocomplete="off" tabindex="1" name="username" value="<%=request.getAttribute("username")==null?"":request.getAttribute("username")%>"/> ```
采用el表达式回显更加简便!
login.jsp代码
</div> <div class="msg_cont"> <b></b> <span class="errorMsg"> ${requestScope.msg} </span> </div> <div class="form">
<input type="hidden" name="action" value="login"> <label>用户名称:</label> <input class="itxt" type="text" placeholder="请输入用户名" autocomplete="off" tabindex="1" name="username" value="${requestScope.username}"/> <br />
regist.jsp代码
<div class="tit"> <h1>注册尚硅谷会员</h1> <span class="errorMsg"> ${ requestScope.msg} </span> </div>
<br /> <label>电子邮件:</label> <input class="itxt" type="text" placeholder="请输入邮箱地址" value="${requestScope.email}" autocomplete="off" tabindex="1" name="email" id="email" /> <br />
讲loginServlet和registServlet整合到一个java文件UserServlet中
public class UserServlet extends HttpServlet { private Service service = new ServiceImpl(); //在这里讲loginServlet和registServlet两个合并到一个java文件中 public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //第一步,先获取输入的值 String username = req.getParameter("username"); String password = req.getParameter("password"); //判度用户 账号,密码是否正确,不正确返回登录页面 /* if (service.exitusername(username)){ //如果存在,登录成功 req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp); }else{ System.out.println("登录失败"); req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp); }*/ User user = service.login(new User(null, username, password, null)); if (user==null){ //如果密码或者用户错误,回显信息 req.setAttribute("msg","你输入的密码错误"); req.setAttribute("username",username); //用来回显账户 //反对登录 req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp); }else{ req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp); } } public void regist(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{ //先获取用户输入的值 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); String code = req.getParameter("code"); //第一步先检查验证码是否正确,不正确返回到注册页面 if (code.equals("abcde")){ if (service.exitusername(username)){ //调用exitusername 如果传入的username存在则返回注册页面 System.out.println("用户名已存在"); //当用户名存在是,回显用户名和邮箱,并且提升错误 req.setAttribute("msg","你输入的用户名重复"); req.setAttribute("username",username); req.setAttribute("email",email); req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); }else{ //用户名不存在 就保存 service.resgit(new User(null,username,password,email)); req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp); } //第二部 如果验证码正确,采用registservce 来帮助我们连接数据库 }else{ System.out.println("你输入的验证码错误"); req.setAttribute("msg","验证码错误"); req.setAttribute("username",username); req.setAttribute("email",email); req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String action = req.getParameter("action"); /* if (action.equals("login")){ login(req, resp); }else if("regist".equals(action)){ regist(req, resp); } */ //采用反射的方法来简化代码 try { Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class); method.invoke(this,req,resp); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
-
第一步,先创建一个UserServlet程序,将他配置到xml文件中,并且修改regist.jsp和login.jsp中form 标签中的 action地址, 在 表单中再加入 <input type="hidden" name="action" value="login">,让UserServlet 中的dopost方法,通过得到action(k值),在得到login(v值),regist.jsp也一样加入 <input type="hidden" name="action" value="login">,让UserServlet 中的dopost方法,通过得到action(k值),在得到login(v值),在通过反射将不同的值,反射给不同的方法
-
//login.jsp <div class="form"> <form action="UserServlet" method="post"> <input type="hidden" name="action" value="login"> <label>用户名称:</label> <input class="itxt" type="text" placeholder="请输入用户名"
<div class="form"> <form action="UserServlet" method="post"> <input type="hidden" name="action" value="regist">
-
因为用户输入的值,可能会很多,每一次输入,都要频繁的接受好多次就像银行的业务一样,所以我们是导入了Beanutils和loggin良jar包
public void regist(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{ //先获取用户输入的值 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); String code = req.getParameter("code"); //上面代码获取用户输入的值,太过麻烦,因为用户输入的值,可能会很多,每一次输入,都要频繁的接受好多次就像银行的业务一样,所以我们是导入了Beanutils和loggin良jar包 User user = new User(); WebUtils.accept(req.getParameterMap(),user);
新建的 WebUtils类中的代码
public static void accept(Map value, Object obj){ //其实可以采用泛型,这样更加简介 //public static <T> T void accept(Map value, T obj) //改成泛型之后,后面调用这个方法的代码就可以写成 System.out.println("输入之前的对象"+obj); try { BeanUtils.populate(obj,value); //这里是将获取到的值,以map集合的方式返回 System.out.println("输入之后的对象"+obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }