您的位置:首页 > 产品设计 > UI/UE

Servlet中The requested resource is not available错误

2015-09-22 08:35 507 查看
自己为了测试servlet,用MyEclipse2015写了一个简单的登录程序。

1.登录页面index.jsp.

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String lUserName = (String)session.getAttribute("iUserName");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="utf-8">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<%
if (lUserName != ""&&lUserName!=null) {
%>
欢迎光临,<%=lUserName%>
<%
} else {
%>
<form name="loginfrm" action="loginserv" method="post">
用户名:<input type="text" name="username" value="zhangsan"><br>
密码:<input type="text" name="password" value="mm123456"><br>
<input type="submit" value="登录">
</form>
<%
}
%>
</body>
</html>


2.用模版写了一个servlet程序loginserv.java.

package com.kaly.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class loginserv extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html);charset=utf-8");
String getUser=request.getParameter("username");
String getpassword=request.getParameter("password");
PrintWriter pw=response.getWriter();
pw.print(getUser);
}

}


这是一个很简单的程序,在收到请求后,把用户名打印出来。
3.运行出现问题:The requested resource is not available.



4.查找咨询,没有解决。后来查看web.xml。

<?xml version="1.0" encoding="UTF-8"?>
<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>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>loginserv</servlet-name>
<servlet-class>com.kaly.servlet.loginserv</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>loginserv</servlet-name>
<url-pattern>/servlet/loginserv</url-pattern>
</servlet-mapping>

</web-app>


因为这些都是自动生成,没有想到过会出现问题。不过记起以前写这个程序时<url-pattern>节点只是在<servlet-name>节点内容前多一个"/",尝试将其改为/loginserv,重启服务运行页面。一些正常。

总结:这应该是这种问题出现的其中一种解决途径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: