点击登陆会出现404 哪里 错了 老师帮看下
package com.imooc.servlet;
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.cus.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 {
//创建一个map值用于保存接收到的数据;
Map<String,String> map=new HashMap<String,String>();
//1创建一个磁盘文件项工厂对象
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
//2创建一个核心解析类
ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
//3解析reqest请求,返回的是List集合,List集合存放的是FileItem对象
List<FileItem> list=servletFileUpload.parseRequest(request);
//定义一个list的集合,用来保存兴趣爱好数据
List<String> hobbyList=new ArrayList<String>();
//4遍历集合,获得每个fileItem,判断是表单项还是文件上传项
String url=null;
for(FileItem fileItem:list) {
//判断是表单项还是文件上传项
if(fileItem.isFormField()) {
//普通表单项
String name=fileItem.getFieldName();//获得表单项name属性都值
String value=fileItem.getString("UTF-8");//获得表单项的值
System.out.println(name+" "+value);
if("hobby".equals(name)) {
String hobbyValue=fileItem.getString("UTF-8");
hobbyList.add(hobbyValue);
//hobbyValue=hobbyList.toString().substring(1,hobbyList.toString().length()-1);
System.out.println(name+" "+hobbyList);
map.put(name,hobbyValue);
}else {
map.put(name,value);
}
}else {
//文件上传
//文件上传功能
//获得文件上传的名字
String filename=fileItem.getName();
if(filename!=null && !"".equals(filename)) {
String uuidFileName=UploadUtils.getUUIDFileName(filename);
//获得文件上传都数据
InputStream is=fileItem.getInputStream();
//获得文件上传的路径
String path=this.getServletContext().getRealPath("/upload");
//将输入流对接到输出流
url=path+"\\"+uuidFileName;
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集合中
List<User> userlist=(List<User>)this.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对象
User userList=new User();
userList.setHobby(map.get("hobby"));
userList.setPassword(map.get("password"));
userList.setNickname(map.get("nickname"));
userList.setSex(map.get("sex"));
userList.setUsername(map.get("username"));
userList.setUsername(map.get(url));
//将注册信息放到List集合中
userlist.add(userList);
for(User u:userlist) {
System.out.println(u);
}
this.getServletContext().setAttribute("list", userlist);
request.getSession().setAttribute("username", userList.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);
}
}
//登陆界面
<%@ 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/log.css">
</head>
<body>
<div class="reg">
<div class="header">
<h1>
<a class="a1" href="./login.jsp">登陆</a>
<a class="a2" href="./regist.jsp">注册</a>
</h1>
</div>
<%
String username="";
if(session.getAttribute("username")!=null){
username=(String)session.getAttribute("username");
}
%>
<form action="" >
<table class="main">
<tr>
<td class="p1">用户名</td>
<td><input class="p2" type="text" name="username" value="<%= username %>"></td>
</tr>
<tr>
<td class="p1">密码</td>
<td><input class="p2" type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input class="p1" type="checkbox" value="1">记住用户名</td>
</tr>
<tr>
<td colspan="2"><input class="p3" type="submit" value="登陆"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
//注册界面
<%@ 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>
<div class="reg">
<div class="header">
<h1>
<a class="a1" href="./login.jsp">登陆</a>
<a class="a2" href="./regist.jsp">注册</a>
</h1>
</div>
<%
String msg="";
if(request.getAttribute("msg")!=null){
msg=(String)request.getAttribute("msg");
}
%>
<h3><font color="red"><%=msg %></font></h3>
<form action="/reg_login/RegistServlet" method="post" enctype="multipart/form-data">
<table class="main">
<tr>
<td class="p1">用户名</td>
<td><input type="text" class="p2" name="username"></td>
</tr>
<tr>
<td class="p1">密码</td>
<td><input type="password" class="p2" name="password"></td>
</tr>
<tr>
<td class="p1">昵称</td>
<td><input type="text" class="p2" name="nickname"></td>
</tr>
<tr>
<td >
<span class="p1">性别</span>
</td>
<td>
<input class="p3" checked type="radio" value="male" name="sex">男
<input type="radio" value="female" name="sex">女
</td>
</tr>
<tr>
<td class="p1">上传图像</td>
<td><input class="p3" type="file" name="upload"></td>
</tr>
<tr>
<td>
<span class="p1">兴趣爱好</span>
</td>
<td>
<input class="p3" type="checkbox" value="篮球" name="bobby">篮球
<input type="checkbox" value="足球" name="bobby">足球
<input type="checkbox" value="羽毛球" name="bobby">羽毛球
<input type="checkbox" value="排球" name="bobby">排球
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" class="p4" value="注册"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
package com.imooc.utils;
import java.util.UUID;
/**
* 文件上传的工作类
* @author
*
*/
public class UploadUtils {
/*
* 生成唯一的文件名
*/
public static String getUUIDFileName(String fileName) {
int idx=fileName.lastIndexOf(".");
String extion=fileName.substring(idx);
String uuidfilename=UUID.randomUUID().toString().replace("-","")+extion;
return uuidfilename;
}
public static void main(String[] args) {
System.out.println(getUUIDFileName("1.jpg"));
}
}
package com.imooc.cus;
//包装类
public class User {
private String username;
private String password;
private String nickname;
private String hobby;
private String sex;
private String path;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", nickname=" + nickname + ", hobby=" + hobby
+ ", sex=" + sex + ", path=" + path + "]";
}
}
package com.imooc.servlet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import com.imooc.cus.User;
/**
* Servlet implementation class InitServlet
*/
public class InitServlet extends HttpServlet {
@Override
public void init() throws ServletException {
// 创建一个list的集合用于保存用户注册信息
List<User> list=new ArrayList<User>();
//将list导入ServletContext作用域中
this.getServletContext().setAttribute("list",list);
}
}
//logcss样式
*{
margin:0;
padding:0;
}
body{
background:rgba(238,238,238,0.5);
position:relative;
font-family:"微软雅黑";
height:100%;
width:100%;
background:lightblue;
}
.reg{
height:400px;
width:500px;
position:absolute;
z-index:1;
margin:100px 100px 100px 350px;
background-color:#fff;
}
.header{
height:50px;
width:450px;
overflow:hidden;
margin-left:25px;
border-bottom:1px solid #bebebe;
}
.header h1{
margin-left:30px;
}
.header a{
font-size:18px;
text-decoration:none;
line-height:60px;
padding-left:10px;
}
.a1{
color:#2c689b;
}
.a2{
color:black;
}
.header a:hover{
color:black;
}
.main{
padding:15px;
height:350px;
width:450px;
font-weight:bolder;
font-size:16px;
margin-top:10px;
margin-left:40px;
}
.p1 .p3{
padding-left:10px;
}
.p2{
width:300px;
height:40px;
line-height: 40px;
border:1px solid #d0d6d9;
background: #F9F9F9;
margin-left:20px;
}
.p3{
width:380px;
height:50px;
background-color:lightgreen;
color:black;
}
//reg.css样式
*{
margin:0;
padding:0;
}
body{
background:rgba(238,238,238,0.5);
position:relative;
font-family:"微软雅黑";
height:100%;
width:100%;
background:lightblue;
}
.reg{
height:auto;
width:500px;
position:absolute;
z-index:1;
margin:100px 100px 100px 350px;
background-color:#fff;
}
.header{
height:50px;
width:450px;
overflow:hidden;
margin-left:25px;
border-bottom:1px solid #bebebe;
}
.header h1{
margin-left:50px;
}
.header a{
font-size:18px;
text-decoration:none;
line-height:60px;
}
.a2{
color:#2c689b;
margin-left:10px;
}
.a1{
color:black;
}
.header a:hover{
color:black;
}
.main{
padding:15px;
height:500px;
width:450px;
font-weight:bolder;
font-size:16px;
margin-top:10px;
margin-left:40px;
}
.p1{
padding-left:10px;
}
.p2{
width:300px;
height:40px;
line-height: 40px;
border:1px solid #d0d6d9;
background: #F9F9F9;
margin-left:20px;
}
.p3{
margin-left:20px;
}
.p4{
width:400px;
height:50px;
background-color:lightgreen;
color:black;
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星