简体   繁体   中英

Java Servlet won't recognize changes in java file after compilation

I'm setting up a java servlet in Tomcat. I managed to set up a servlet and get it to print out html code by overriding the doGet() method in my servlet java file. However, I soon came to realize that after I had made the servlet, no matter how much I changed the java file and compiled it into the right folder, it did not change the behavior of the servlet at all. I have been restarting Tomcat every time using ./startup.sh - is there something more I need to do to reconfigure Tomcat every time? I checked the location of the class (CATALINA_HOME/webapps/helloservlet/WEB_INF/mypkg/HelloServlet.class), which I correctly specify in web.xml, to see if the .class file had been modified since I compiled the .java file, and it had. The content of the web.xml file are as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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">

<!-- To save as <CATALINA_HOME>\webapps\helloservlet\WEB-INF\web.xml -->

<servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>mypkg.HelloServlet</servlet-class>
</servlet>

<!-- Note: All <servlet> elements MUST be grouped together and
     placed IN FRONT of the <servlet-mapping> elements -->

<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/sayhello</url-pattern>
</servlet-mapping>

The changes I made to the java file are not trivial; it used to print out a whole block of html code and I changed it to print a single line of plaintext. When I type in the URL into the browser it still prints out all the html code. I will happily provide any more code if needed.

For what it's worth, I found a really weird solution. It seems that the web.xml file, because the name was the same, didn't recognize the updated .class file. What I did was I literally just deleted one character in web.xml and retyped it, then saved web.xml, as if to "modify" it, even though I didn't really change the contents. This made it recognize the new, updated .class file and it now works. I hope this might help someone with the same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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