我向其添加context-parameters时web.xml文件出错

时间:2016-08-26 19:12:06

标签: java xml servlets

我在向web.xml文件添加上下文参数时收到错误 这是web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
  <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<context-param>
    <pram-name>ADMIN_PATH</param-name>
    <param-value>AdminChatServlet</param-value>
</context-param>      
<context-param>
    <param-name>ROOMLIST_PATH</param-name>
    <param-value>/RoomListServlet</param-value> 
</context-param>    
<context-param>
    <param-name>CHROOM_PATH</param-name>
    <param-value>/ChRoomServlet</param-value>
</context-param>  
<servlet>
    <servlet-name>MainChatServlet</servlet-name>
    <servlet-class>MainChatServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>AdminChatServlet</servlet-name>
    <servlet-class>AdminChatServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>RoomListServlet</servlet-name>
    <servlet-class>RoomListServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>ChRoomServlet</servlet-name>
    <servlet-class>ChRoomServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MainChatServlet</servlet-name>
    <url-pattern>/MainChatServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>AdminChatServlet</servlet-name>
    <url-pattern>/AdminChatServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>RoomListServlet</servlet-name>
    <url-pattern>/RoomListServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ChRoomServlet</servlet-name>
    <url-pattern>/ChRoomServlet</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
</web-app>

这是使用这些上下文参数的servlet(MainChatServlet):

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;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import com.amir.*;
public class MainChatServlet extends HttpServlet {

 String chRoomPath;//="ChRoomServlet.java";
 String roomListPath;//="RoomListServlet.java";
 String adminChatPath;//="AdminChatServlet.java";

 public void init()
 {
 ServletContext context = getServletConfig().getServletContext();
 context.setAttribute("chRoomPath",context.getInitParameter("CHROOM_PATH"));
 context.setAttribute("roomListPath",context.getInitParameter("ROOMLIST_PATH"));
context.setAttribute("adminChatPath",context.getInitParameter("ADMINCHAT_PATH"));
 }

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

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,  IOException
{       
   HttpSession session =  request.getSession();

   chRoomPath = (String)getServletContext().getAttribute("chRoomPath");
   roomListPath = (String)getServletContext().getAttribute("roomListpath");
   adminChatPath = (String)getServletContext().getAttribute("adminChatPath");

   session.setAttribute("chRoomPath",chRoomPath);
   session.setAttribute("roomListPath", roomListPath);
   session.setAttribute("adminChatPath",adminChatPath);

   HashMap hashmap = null;
   try
   {
      Class.forName("com.mysql.jdbc.Driver");
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/chat","root","mysql");

      synchronized(getServletContext())
              {
                hashmap = (HashMap)getServletContext().getAttribute("chatList");
                if(hashmap == null)
                {
                  hashmap =new HashMap();
                  Statement stmt = conn.createStatement();
                  ResultSet rs = stmt.executeQuery("select * from chatrooms");
                  while(rs.next())
                  {
                    hashmap.put(rs.getString(1),new ChatRoom(rs.getString(1),rs.getString(2),4));                        
                  }
                  rs.close();
                  getServletContext().setAttribute("roomList", hashmap);
                }
               }    
      conn.close();
   }
   catch(ClassNotFoundException e)
   {
      System.out.print("Error(Class)");
      e.printStackTrace();
   }
   catch(SQLException e)
   {
      System.out.print("Error(SQL)");
      e.printStackTrace();
   }
   RequestDispatcher view = request.getRequestDispatcher("chat.jsp");
   view.forward(request, response);

  }
}

这里是default-package,其中包含所有.java文件(保存servlet)

截图#1

为什么我在web.xml文件中收到错误?

截图#2

编辑:或者如果可能的话,建议我任何其他想法。

0 个答案:

没有答案