HTTP servlet和jsp 404
我有这种疾病的时候我编程这样的:HTTP servlet和jsp 404
HTTP状态404 -/IT/UserServlet
类型报告“
状态留言/ IT/UserServlet
描述所请求的资源不可用。
的Apache Tomcat/8.0.27
这是我的代码: register.jsp
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register Page</title>
<script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/register_style.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var x_timer;
$("#username").keyup(function (e) {
clearTimeout(x_timer);
var user_name = $(this).val();
x_timer = setTimeout(function() {
check_username_ajax(user_name);
}, 1000);
});
function check_username_ajax(username) {
$("#user-result").html('<img src="img/ajax-loader.gif" />');
$.post('CheckUserNameServlet', {'username': username}, function (data) {
$("#user-result").html(data);
});
}
});
</script>
</head>
<body>
<form action="UserServlet" method="POST">
<h1>Sign up</h1><br/>
<span class="input"></span>
<input type="text" name="name" id = "name" placeholder="Full name" autofocus autocomplete="off" />
<span class="input" ></span>
<input type="text" name="username" id="username" placeholder="username" />
<span id = "user-result"></span>
<span id="passwordMeter"></span>
<input type="password" name="password" id="password" placeholder="Password" title="Password min 8 characters. At least one UPPERCASE and one lowercase letter" required pattern="(?=^.{8,}$)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$"/>
<input type="hidden" value="insert" name="command">
<button type="submit" value="Sign Up" title="Submit form" class="icon-arrow-right"><span>Sign up</span></button>
</form>
</body>
这是sevlet:
public class UserServlet extends HttpServlet { UserDAO userDao = new UserDAO();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
String command = request.getParameter("command");
String url="";
switch(command){
case"insert":
User user = new User();
user.setUserId(new Date().getTime());
user.setName(request.getParameter("name"));
user.setUserName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setCreateTime(Date.from(Instant.now()));
user.setUpdateTime(Date.from(Instant.now()));
userDao.InsertUser(user);
HttpSession session = request.getSession();
session.setAttribute("user", user);
url = "index.jsp";
break;
}
RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
rd.forward(request, response);
}
}
这是UseDAO:
public boolean InsertUser(User u) { Connection conn = Connect.getConnecttion();
String insert_user = "INSERT INTO user(name, user_name, password, role, create_time, update_time) values(?,?,?,?,?,?)";
try {
PreparedStatement ps = conn.prepareStatement(insert_user);
ps.setString(1, u.getName());
ps.setString(2, u.getUserName());
ps.setString(3, u.getPassword());
ps.setInt(4, 4);
ps.setDate(5, Date.valueOf(LocalDate.now()));
ps.setDate(6, Date.valueOf(LocalDate.now()));
ps.executeUpdate();
return true;
} catch (SQLException ex) {
Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
我的web.xml:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>Controller.UserServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>CheckUserNameServlet</servlet-name>
<servlet-class>Controller.CheckUserNameServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CheckUserNameServlet</servlet-name>
<url-pattern>/CheckUserNameServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
当我运行它,并测试register.jsp告诉我的错误,但我不知道在哪里,所以请大家帮忙!
回答:
基础上添加的web.xml中,你-已映射的URL /LoginServlet
,你是不是/UserServlet
尝试尝试访问。
另外,还要安全,阙拉上下文路径(/IT
)就在你的服务器。
以上是 HTTP servlet和jsp 404 的全部内容, 来源链接: utcz.com/qa/257807.html