I am absolutely new in servlet technology and it is absolutely basic question, but I get confused In the tutorial which are very complex for me. I have a new servicelet So far, the servlet is loaded using jetettes, I am happy, I will call "Hello World" to Now, I want the sublet to be able to respond to GET requests, say, How can I get it? First of all, if you want to respond to POST requests, then you Second, I I recommend thinking about handling each URL in a different service, as long as your code will not be as easy as the example. It is likely that during the development of real-world applications, your code It will be more complex, so if you separate the responsibilities in two servlets, then it will be very clean. If you agree with this approach, then it is only one more The request for Your idea (comparing the contents of the URL inside the servlet) also works, but this is not a good design because you end up with a large if / or / chain Think about this, which seems like bad, in this case. HelloWorldServlet . In
web.xml , I have it
& lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt ;! DOCTYPE Web-App Public "- // Sun Microsystems, Inc. /// DTD Web Application 2.2 / n" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> & Lt; Web Apps & gt; & Lt; Servlet & gt; & Lt; Servlet-name & gt; HelloWorldServlet & lt; / Servlet-name & gt; & Lt; Servlet category & gt; Cz.hello.HelloWorldServlet & lt; / Servlet category & gt; & Lt; / Servlet & gt; & Lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; HelloWorldServlet & lt; / Servlet-name & gt; & Lt; URL pattern & gt; / HelloWorldServlet & lt; / URL pattern & gt; & Lt; / Servlet-mapping & gt; & Lt; / Web application & gt;
HelloWorldServlet.scala (I like to scale Java) looks like this
package cz.hello import javax .servlet Http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse class HelloWorldServlet HttpServlet {Override Def doGet (req: HTTPSvirtiVeSt, resp: HTTPSWriteration) Expands = {resp.setContentType ("text / plain ") Resp. GetWriter.println ("hello, world")}}
Can see at http: // local host: 8080 / HelloWorldServlet .
http: // localhost: 8080 / HelloWorldServlet / Hello and
http: / / Localhost: 8080 / HelloWorldServlet / Bye and differently for both of them For example (pseudocode)
override def doGet (req: httppservatyuse, resp: HTTP Servate Response) = {resp.setContentType ("text / plain") if (req.isAddress ("/ hello ") {Resp.getWriter.println (" hello, world ")} other {resp.getWriter.println (" bye, world ")}}
doPost instead of the method,
doGet .
& lt; servlet & gt; More NY
& lt; Servlet-maping & gt; The thing to do in your
web.xml is as follows:
& lt; Web-app & gt; & Lt; Servlet & gt; & Lt; Servlet-name & gt; HelloWorldServlet & lt; / Servlet-name & gt; & Lt; Servlet category & gt; Cz.hello.HelloWorldServlet & lt; / Servlet category & gt; & Lt; / Servlet & gt; & Lt; Servlet & gt; & Lt; Servlet-name & gt; GoodbyeWorldServlet & lt; / Servlet-name & gt; & Lt; Servlet category & gt; Cz.hello.GoodbyeWorldServlet & lt; / Servlet category & gt; & Lt; / Servlet & gt; & Lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; HelloWorldServlet & lt; / Servlet-name & gt; & Lt; URL pattern & gt; / Hello & lt; / URL pattern & gt; & Lt; / Servlet-mapping & gt; & Lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; GoodbyeWorldServlet & lt; / Servlet-name & gt; & Lt; URL pattern & gt; / Goodbye & lt; / URL pattern & gt; & Lt; / Servlet-mapping & gt; & Lt; / Web application & gt;
/ Hello> will be managed by
HelloWorldServlet , and
/ bye Requested will be governed by
Goodbye World Search. Now it's only a matter of deciding whether GET or POST makes more sense for you and related methods (
doGet or
doPost or both) on your servlet Implementing
Comments
Post a Comment