校验用户名,一直回到注册页面
package com.imooc.servlet; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.imooc.reg.User; import com.imooc.utils.UploadUtils; /** * Servlet implementation class RegistServlet */ @WebServlet("/RegistServlet") public class RegistServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.创建磁盘文件工厂对象 DiskFileItemFactory disFileItemFactory = new DiskFileItemFactory(); //2.创建一个核心解析类 ServletFileUpload servletFileUpload = new ServletFileUpload(disFileItemFactory); //3.解析request,解析的结果是List,里面存放FileItem。 List<FileItem> list = servletFileUpload.parseRequest(request); //3.1实例化,接受复选表单 List<String> hobbyList = new ArrayList(); //3.2实例化,保存接受数据 Map<String,String> map = new HashMap<String,String>(); //4.遍历FileItem String url=null; User user = new User(); for(FileItem fileItem:list) { //判断是表单项还是文件项 if(fileItem.isFormField()) { String name = fileItem.getFieldName();//获得表单属性的值 String value = fileItem.getString("UTF-8");//获得表单的值 //System.out.println(name+" "+value); if("hobby".equals(name)) { hobbyList.add(value); String hl = hobbyList.toString(); //System.out.println(name+" "+hl.substring(1, hl.length()-1)); map.put(name, hl); }else{ map.put(name, value); } }else { //文件上传项 //获得文件名 String fileName = fileItem.getName(); if(fileName!=null&&!"".equals(fileName)) { //获得唯一文件名 String uuidName = UploadUtils.getUUIDFileName(fileName); //获得文件上传数据 InputStream is = fileItem.getInputStream(); //获得上传路径 String path = this.getServletContext().getRealPath("/upload"); //如果没有对应文件夹,创建path路劲文件夹 File file = new File(path); System.out.println(path); boolean isExist = file.exists(); if(isExist==false) { file.mkdirs(); } url = path+"\\"+uuidName; //输入输出流对接 OutputStream os = new FileOutputStream(url); int len = 0; byte[] b = new byte[1024]; while((len=is.read(b))!=-1) { os.write(b, 0, len); } is.close(); os.close(); } } System.out.println(map); //校验用户名是否存在 List<User> userList = (List<User>) request.getServletContext().getAttribute("list"); for(User u :userList) { if(u.getName().equals(map.get("username"))) { request.setAttribute("msg", "用户名存在"); request.getRequestDispatcher("/regist.jsp").forward(request,response); return; } } //封装接收的信息 user.setName(map.get("username")); user.setPassword(map.get("password")); user.setNincname(map.get("nickname")); user.setSex(map.get("sex")); user.setHobby(map.get("hobby")); user.setPath(url); userList.add(user); this.getServletContext().setAttribute("list", userList); } //跳转 request.getSession().setAttribute("username", user.getName()); response.sendRedirect(request.getContextPath()+"/login.jsp"); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
重新启动服务器,注册信息,点击注册就会跳到注册页面,并显示用户名已存在。哪里有问题?
0
收起
正在回答
3回答
同学你好,同学的括号有些问题,在is.close();和os.close();后应该是三个大括号,跳转的前面没有大括号,同学修改一些再试试。代码如下:
public class RegistServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.创建磁盘文件工厂对象 DiskFileItemFactory disFileItemFactory = new DiskFileItemFactory(); //2.创建一个核心解析类 ServletFileUpload servletFileUpload = new ServletFileUpload(disFileItemFactory); //3.解析request,解析的结果是List,里面存放FileItem。 List<FileItem> list = servletFileUpload.parseRequest(request); //3.1实例化,接受复选表单 List<String> hobbyList = new ArrayList(); //3.2实例化,保存接受数据 Map<String,String> map = new HashMap<String,String>(); //4.遍历FileItem String url=null; User user = new User(); for(FileItem fileItem:list) { //判断是表单项还是文件项 if(fileItem.isFormField()) { String name = fileItem.getFieldName();//获得表单属性的值 String value = fileItem.getString("UTF-8");//获得表单的值 //System.out.println(name+" "+value); if("hobby".equals(name)) { hobbyList.add(value); String hl = hobbyList.toString(); //System.out.println(name+" "+hl.substring(1, hl.length()-1)); map.put(name, hl); }else{ map.put(name, value); } }else { //文件上传项 //获得文件名 String fileName = fileItem.getName(); if(fileName!=null&&!"".equals(fileName)) { //获得唯一文件名 String uuidName = UploadUtils.getUUIDFileName(fileName); //获得文件上传数据 InputStream is = fileItem.getInputStream(); //获得上传路径 String path = this.getServletContext().getRealPath("/upload"); //如果没有对应文件夹,创建path路劲文件夹 File file = new File(path); System.out.println(path); boolean isExist = file.exists(); if(isExist==false) { file.mkdirs(); } url = path+"\\"+uuidName; //输入输出流对接 OutputStream os = new FileOutputStream(url); int len = 0; byte[] b = new byte[1024]; while((len=is.read(b))!=-1) { os.write(b, 0, len); } is.close(); os.close(); } } } System.out.println(map); //校验用户名是否存在 List<User> userList = (List<User>) request.getServletContext().getAttribute("list"); for(User u :userList) { if(u.getUsername().equals(map.get("username"))) { request.setAttribute("msg", "用户名存在"); request.getRequestDispatcher("/regist.jsp").forward(request,response); return; } } //封装接收的信息 user.setUsername(map.get("username")); user.setPassword(map.get("password")); user.setNickname(map.get("nickname")); user.setSex(map.get("sex")); user.setHobby(map.get("hobby")); user.setPath(url); userList.add(user); this.getServletContext().setAttribute("list", userList); //跳转 request.getSession().setAttribute("username", user.getUsername()); response.sendRedirect(request.getContextPath()+"/login.jsp"); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!
慕粉0194161
2018-11-29 11:53:40
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注册</title> <link rel="stylesheet" href="./css/reg.css"> </head> <body> <% String msg=""; if(request.getAttribute("msg")!=null){ msg=(String)request.getAttribute("msg"); } %> <div class="reg"> <div class="header"> <h1> <a href="./login.jsp">登录</a> <a href="./regist.jsp">注册</a> </h1> </div> <h1><font color="red"><%=msg %></font></h1> <form action="/reg_login/RegistServlet" method="post" enctype="multipart/form-data"> <table> <tr> <td class="td1">用户名</td> <td><input type="text" class="input1" name="username"></td> </tr> <tr> <td class="td1">密码</td> <td><input type="password" class="input1" name="password"></td> </tr> <tr> <td class="td1">昵称</td> <td><input type="text" class="input1" name="nickname"></td> </tr> <tr> <td class="td1">性别</td> <td> <input type="radio" name="sex" value="male">男 <input type="radio" name="sex" value="female">女 </td> </tr> <tr> <td class="td1">上传头像</td> <td><input type="file" id="photo" name="upload"></td> </tr> <tr> <td class="td1">兴趣爱好</td> <td><label> <input type="checkbox" name="hobby" value="篮球">篮球 <input type="checkbox" name="hobby" value="足球">足球 <input type="checkbox" name="hobby" value="排球">排球 <input type="checkbox" name="hobby" value="羽毛球">羽毛球 </label></td> </tr> <tr> <td colspan="2"> <div class="btn-red"> <input type="submit" value="注册" id="reg-btn"> </div> </td> </tr> </table> </form> </div> </body> </html>
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星