How to start a Java servlet?

I have a servlet. I am new to java. But I need to run the servlet. It has two methods:

public void doGet (HttpServletRequest request, HttpServletResponse response) {...} 

and

 public void doPost HttpServletRequest request, HttpServletResponse response) {...} 

What steps do I need to take to run the servlet? (I have tomcat 7, eclipse SE installed with the tomcat plugin, netBeans)

+4
source share
7 answers
  • Creating a dynamic web project
  • Create a new class that extends HttpServlet and override the doGet and doPost methods, write your business logic there
  • Set up web.xml , something like:

      <servlet> <servlet-name>helloworld</servlet-name> <servlet-class>test.helloworld</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloworld</servlet-name> <url-pattern>/helloworld</url-pattern> </servlet-mapping> 
  • Deploy your web project in tomcat

  • Enter localhost:8080/mywebapp/helloworld.do in the address bar of the browser, mywebapp is your project name

If you are lucky, you will see the result.

+6
source

I suggest you:

  • Open netbeans and create a new web project.
  • Right click on project, add servlet
  • Right-click the project and select Run. He will launch the Glassfish web application.
  • It will automatically open your web browser and go to the servlet address, for example: localhost: 8080 / MyServlet, etc.

This is the fastest way to start a servlet. enjoy.

+3
source

Create a java web project with an IDE (Netbeans / eclipse), add a servlet to the project, it will simplify your life

+2
source

You don't seem to know much about Java EE and servlets.

Basically, you need to write a web.xml file that will display the URL of your servlet, build a project, create a web archive (WAR), deploy it to the server.

Here is the official guide from Oracle: http://docs.oracle.com/javaee/6/tutorial/doc/bnadp.html .

Try Google to use servlets in tomcat, you will surely find a good tutorial on this.

+2
source

This is a very simple question, man!

You can learn how to do this on Eclipse using the Tutorial Link .

Try to learn from some good books. There are many great Java EE books available on the market.

Or you can also learn Java EE from oracle site .

+2
source

The internal call to doGet and doPost will reach, as shown below,

 Client ----------------------------> Container sends request | | Creates HttpServletRequest HttpServletResponse objects | | Create Thread for that Servlet and pass above objects to it | | Thread Call the Service() method and decision is made to call doGet() or doPost() | | doGet()/doPost() called 
+2
source
+1
source

Source: https://habr.com/ru/post/1436311/


All Articles