java 如何运行java servlet?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12596833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run java servlet?
提问by Dmytro Plekhotkin
I have a servlet. I'm new in java. But i need to run the servlet. It has two methods:
我有一个 servlet。我是 Java 新手。但我需要运行 servlet。它有两种方法:
public void doGet (HttpServletRequest request,
HttpServletResponse response) {...}
and
和
public void doPost HttpServletRequest request,
HttpServletResponse response) {...}
What steps i need to do to run the servlet?(I have tomcat 7 installed, eclipse SE with tomcat plugin, netBeans)
我需要执行哪些步骤来运行 servlet?(我已经安装了 tomcat 7,使用 tomcat 插件,netBeans eclipse SE)
采纳答案by Foredoomed
- Create a dynamic web project
- Create a new class extending
HttpServlet
and override the methoddoGet
anddoPost
, write your business logic in there Configure
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 the tomcat
- Type
localhost:8080/mywebapp/helloworld.do
in the browser's address bar,mywebapp
is your project name
- 创建动态 Web 项目
- 创建延伸的新类
HttpServlet
和覆盖的方法doGet
和doPost
,在写你的业务逻辑有 配置
web.xml
,类似于:<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>
在 tomcat 中部署您的 web 项目
- 键入
localhost:8080/mywebapp/helloworld.do
在浏览器的地址栏,mywebapp
为您的项目名称
If you are lucky, you will see the result.
如果幸运的话,你会看到结果。
回答by Jayesh
Internally call to doGet and doPost will reach like below,
内部调用 doGet 和 doPost 将达到如下所示,
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
回答by Burhan ARAS
I suggest you that :
我建议你:
- Open netbeans and create a new web project
- Right click the project, add a Servlet
- Right click the project and select Run. It will run web app on Glassfish.
- It will automatically open your web browser and navigate to servlet address like : localhost:8080/MyServlet etc.
- 打开 netbeans 并创建一个新的 Web 项目
- 右键项目,添加一个Servlet
- 右键单击项目并选择运行。它将在 Glassfish 上运行 Web 应用程序。
- 它将自动打开您的 Web 浏览器并导航到 servlet 地址,例如:localhost:8080/MyServlet 等。
This is the quickest way to run a servlet. have fun.
这是运行 servlet 的最快方法。玩得开心。
回答by Karthikeyan Arumugam
This is very basic question dude!
这是非常基本的问题,伙计!
You can learn how to do it on Eclipse with this Tutorial link.
您可以通过此教程链接了解如何在 Eclipse 上执行此操作。
Please try learning from some nice books. Many nice Java EE books are available on the market.
请尝试从一些好书中学习。市场上有许多不错的 Java EE 书籍。
Or you can learn Java EE from the oracle Siteas well.
或者您也可以从oracle 站点学习 Java EE 。
回答by Jigar Joshi
Create a java web project with a IDE (Netbeans/eclipse) add a servlet to the project, It will make your life easier
使用IDE(Netbeans/eclipse)创建一个java web项目,在项目中添加一个servlet,它会让你的生活更轻松
回答by mhaligowski
It seems that you know little about Java EE and Servlets.
您似乎对 Java EE 和 Servlet 知之甚少。
Basically, you need to write a web.xml file, which will map an URL to your servlet, build the project, create a web archive (WAR), deploy it on server.
基本上,您需要编写一个 web.xml 文件,该文件会将 URL 映射到您的 servlet、构建项目、创建 Web 存档 (WAR)、将其部署到服务器上。
Here's the official manual from Oracle: http://docs.oracle.com/javaee/6/tutorial/doc/bnadp.html.
这是 Oracle 的官方手册:http: //docs.oracle.com/javaee/6/tutorial/doc/bnadp.html。
Try to google for using servlets on tomcat, you will surely find a good tutorial on that.
尝试谷歌在 tomcat 上使用 servlet,你一定会找到一个很好的教程。