自己为了测试servlet,用MyEclipse2015写了一个简单的登录程序。

1.登录页面index.jsp.

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String lUserName = (String)session.getAttribute("iUserName");
 4 %>
 5 
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     <meta charset="utf-8">
10     <title>My JSP 'index.jsp' starting page</title>
11     <meta http-equiv="pragma" content="no-cache">
12     <meta http-equiv="cache-control" content="no-cache">
13     <meta http-equiv="expires" content="0">    
14     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
15     <meta http-equiv="description" content="This is my page">
16   </head>
17   
18   <body>
19     <%
20         if (lUserName != ""&&lUserName!=null) {
21     %>
22     欢迎光临,<%=lUserName%>
23     <%
24         } else {
25     %>
26     <form name="loginfrm" action="loginserv" method="post">
27      用户名:<input type="text" name="username" value="zhangsan"><br>
28     密码:<input type="text" name="password" value="mm123456"><br> 
29     <input type="submit" value="登录">
30     </form>
31  <%
32      }
33  %>
34   </body>
35 </html>

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

 1 package com.kaly.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class loginserv extends HttpServlet {
12     public void doGet(HttpServletRequest request, HttpServletResponse response)
13             throws ServletException, IOException {
14         doPost(request, response);
15     }
16 
17     public void doPost(HttpServletRequest request, HttpServletResponse response)
18             throws ServletException, IOException {
19         request.setCharacterEncoding("utf-8");
20         response.setContentType("text/html);charset=utf-8");
21         String getUser=request.getParameter("username");
22         String getpassword=request.getParameter("password");
23         PrintWriter pw=response.getWriter();
24         pw.print(getUser);
25     }
26 
27 }

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

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 6   <servlet>
 7     <description>This is the description of my J2EE component</description>
 8     <display-name>This is the display name of my J2EE component</display-name>
 9     <servlet-name>loginserv</servlet-name>
10     <servlet-class>com.kaly.servlet.loginserv</servlet-class>
11   </servlet>
12 
13   <servlet-mapping>
14     <servlet-name>loginserv</servlet-name>
15     <url-pattern>/servlet/loginserv</url-pattern>
16   </servlet-mapping>
17 
18 </web-app>

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

总结:这应该是这种问题出现的其中一种解决途径。