简体   繁体   English

如何在web.xml中添加类?

[英]How to add classes in web.xml?

I made a login form with this lesson: http://bit.ly/eEcVrE 我用这节课做了一个登录表: http//bit.ly/eEcVrE

I install tomcat and mysql, create database and tables. 我安装tomcat和mysql,创建数据库和表。 But i cant understanding web.xml. 但我无法理解web.xml。 Advise how to fill it, or give references to tutorials about web.xml 建议如何填写它,或提供有关web.xml的教程的参考

Login.jsp starts, but an error when i fill field username and password: The requested resource (/project1/LoginServlet) is not available. Login.jsp启动,但填写字段用户名和密码时出错:请求的资源(/ project1 / LoginServlet)不可用。

sorry for very-very stupid question, but i really dont know what do. 抱歉非常非常愚蠢的问题,但我真的不知道该做什么。 and sorry for bad english. 抱歉英语不好。 Thanks. 谢谢。

You don't actually add classes to the web.xml, nor do you need to indicate their locations within the web.xml file. 您实际上并没有向web.xml添加类,也不需要在web.xml文件中指明它们的位置。

Add them to the WEB-INF/classes sub-directory of your war file (in their appropriate hierchial directory nesting), or if the classes are already packed into JAR files, add them to the WEB-INF/lib sub-directory of your war file. 将它们添加到war文件的WEB-INF / classes子目录中(在其相应的hierchial目录嵌套中),或者如果这些类已经打包到JAR文件中,则将它们添加到您的WEB-INF / lib子目录中战争档案。

The web.xml file contains two very important maps. web.xml文件包含两个非常重要的映射。

  1. It maps the URL pattern to a Servlet Name. 它将URL模式映射到Servlet名称。
  2. It maps the Servlet Name to a class. 它将Servlet名称映射到一个类。

Once you are sure the class is within the WAR file, you need to verify that you are calling the correct URL. 一旦确定该类在WAR文件中,您需要验证是否正在调用正确的URL。 An entry like 像这样的条目

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

means that calling 意味着打电话

http://myserver:port/webappName/HelloServlet

will be redirected to the "HelloServlet" servlet. 将被重定向到“HelloServlet”servlet。 An entry like 像这样的条目

 <servlet>
     <servlet-name>HelloServlet</servlet-name>
     <servlet-class>mypackage.HelloServlet</servlet-class>
 </servlet>

will make sure that all requests to the "HelloServlet" servlet are directed to the class mypackage.HelloServlet , which might be located in the WAR's internal WEB-INF\\classes\\mypackage\\HelloServlet.class . 将确保对“HelloServlet”servlet的所有请求都定向到类mypackage.HelloServlet ,该类可能位于WAR的内部WEB-INF\\classes\\mypackage\\HelloServlet.class Alternatively it might be located in the WAR's internal WEB-INF\\lib\\myjar.jar file, provided the myjar.jar file contains a mypackage\\HelloServlet.class file. 或者,它可能位于WAR的内部WEB-INF\\lib\\myjar.jar文件中,前提是myjar.jar文件包含mypackage\\HelloServlet.class文件。

Good luck! 祝好运!

web.xml is web - application descriptor. web.xml是web - 应用程序描述符。

The web.xml file provides configuration and deployment information for the Web components that comprise a Web application. web.xml文件提供组成Web应用程序的Web组件的配置和部署信息。


Login.jsp starts, but an error when i fill field username and password: The requested resource (/project1/LoginServlet) is not available. Login.jsp启动,但填写字段用户名和密码时出错:请求的资源(/ project1 / LoginServlet)不可用。

It is because you haven't mapped this URL pattern in web.xml 这是因为您尚未在web.xml中映射此URL模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM