老师,启动服务器有问题了
package com.imooc.domain;
public class User {
private String username;
private String password;
private String nickname;
private String sex;
private String hobby;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
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 + ", sex=" + sex
+ ", hobby=" + hobby + ", path=" + path + "]";
}
}
package com.imooc.utils;
import javax.servlet.http.Cookie;
public class CookieUtils {
public static Cookie findCookie(Cookie[] cookies ,String name){
if(cookies == null){
// 璇存槑瀹㈡埛绔病鏈夋惡甯ookie:
return null;
}else{
// 璇存槑瀹㈡埛绔惡甯ookie:
for (Cookie cookie : cookies) {
if(name.equals(cookie.getName())){
return cookie;
}
}
return null;
}
}
}
package 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.domain.User;
/**
* 用户注册初始化的Servlet
*/
@WebServlet("/InitServlet")
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
//创建list集合用于保存用户注册信息
List<User> list=new ArrayList ();
//将Servlet保存在ServletContext作用域中
this.getServletContext().setAttribute("list", list);
}
}
package Servlet;
import java.io.IOException;
import java.util.List;
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;
/**
* 用户注册
*/
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//数据的接收
//文件上传基本操作:
try {
//1.创建一个磁盘文件项工厂对象
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
//2.创建一个核心解析类
ServletFileUpload servletFileUpload=new ServletFileUpload();
servletFileUpload.parseRequest(request);
//3.解析request请求
List <FileItem> list=servletFileUpload.parseRequest(request);
//4.遍历集合,获得每个FileItem,判断是表单项还是文件上传项
for(FileItem fileItem:list) {
//判断是表单项还是文件上传项
if(fileItem.isFormField()) {
//普通表单项
//接收表单项参数值
String name=fileItem.getFieldName();
String value=fileItem.getString("UTF-8");
System.out.println(name+" "+value);
}else {
//文件上传项
//文件上传功能
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ page import="com.imooc.utils.CookieUtils" %>
<%@ 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/login.css">
</head>
<body>
<div class="login">
<div class="header">
<h1>
<a href="./login.jsp">登录</a> <a href="./regist.jsp">注册</a>
</h1>
</div>
<%
String username="";
// 获得从客户端携带过来的所有的Cookie
Cookie[] cookies = request.getCookies();
// 从Cookie的数组中查找指定名称的Cookie
Cookie cookie =CookieUtils.findCookie(cookies, "username");
if(cookie != null){
username = cookie.getValue();
}
if(session.getAttribute("username")!=null){
username = (String)session.getAttribute("username");
}
String msg = "";
if(request.getAttribute("msg")!=null){
msg = (String)request.getAttribute("msg");
}
%>
<h3><font color="red"><%=msg %></font></h3>
<form action="/reg_login/LoginServlet" method="post">
<table>
<tr>
<td class="td1">用户名</td>
<td><input type="text" class="input1" name="username" value="<%=username %>"></td>
</tr>
<tr>
<td class="td1">密码</td>
<td><input type="password" class="input1" name="password"></td>
</tr>
<tr>
<td class="td1" colspan="2">
<input type="checkbox" name="remember" value="true" checked="checked"> 记住用户名</td>
</tr>
<tr>
<td colspan="2">
<div class="btn-red">
<input type="submit" value="登录" id="login-btn">
</div>
</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 href="./login.jsp">登录</a> <a href="./regist.jsp">注册</a>
</h1>
</div>
<!--
文件上传的条件
* 表单必须是post提交方式
* 表单中必须有文件上传项,文件上传项必须有name属性和值
* 表单的enctype属性必须设置为multipart/form-data
-->
<%
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>
<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>
<%@page import="com.imooc.domain.User"%>
<%@ 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/login.css">
</head>
<body>
<%
if(session.getAttribute("user")!=null){
User user = (User)session.getAttribute("user");
System.out.println(user.getPath());
// 获得绝对路径最后一个\的位置
int idx = user.getPath().lastIndexOf("\\");
// 获得文件上传的唯一文件名:
String fileName = user.getPath().substring(idx+1);
%>
<div class="login">
<div class="header">
<h1>登录成功</h1>
</div>
<div class="content">
<table align="center">
<tr>
<td align="center"><img src="/reg_login/upload/<%= fileName %>" /></td>
</tr>
<tr>
<td align="center">欢迎<%= user.getNickname() %>,登录成功!</td>
</tr>
</table>
</div>
</div>
<%
}else{
%>
<div class="login">
<div class="header">
<h1>您还没有登录!请先去<a href="/reg_login/login.jsp">登录</a>!</h1>
</div>
</div>
<%
}
%>
</body>
</html>
正在回答
404代表请求的资源地址不对,说明没有访问到注册的Servlet
请同学截图一下报404的页面截图(包括url的地址),请问同学使用的是idea还是eclipse呢?
如果是idea,默认是不需要加项目名的,所以链接中的项目名需要去掉
如果是eclipse,请同学检查一下,与你写的项目名是否一致呢?
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
老师,将xml修改成如下以后,可以启动服务器,但是浏览器提交注册页面后报错404,且控制台没有打印name value
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Myreg_login</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Myreg_login]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:942)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1423)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1413)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Myreg_login]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
... 6 more
Caused by: java.lang.IllegalArgumentException: The servlets named [InitServlet] and [Servlet.InitServlet] are both mapped to the url-pattern [/InitServlet] which is not permitted
at org.apache.tomcat.util.descriptor.web.WebXml.addServletMappingDecoded(WebXml.java:339)
at org.apache.tomcat.util.descriptor.web.WebXml.addServletMapping(WebXml.java:332)
at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2395)
at org.apache.catalina.startup.ContextConfig.processClass(ContextConfig.java:2071)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2060)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1952)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1946)
at org.apache.catalina.startup.ContextConfig.processClasses(ContextConfig.java:1220)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1134)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:769)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星