在 Tomcat 下运行第一个Servlet
2013-09-15 13:38
316 查看
前提,配好JDK环境,下载6.0以上版本Tomcat绿色版。
新建文件夹:...apache-tomcat-6.0.37\webapps\myApp
新建文件夹:...myApp\WEB-INF
新建文件夹:...WEB-INF/classes
新建文件: ...WEB-INF/web.xml
web.xml
LoginServlet.java
LoginServlet.java依赖于servlet-api.jar,可以在...apache-tomcat-6.0.37\lib下找到。
将LoginServlet.java编译后的LoginServlet.class放到WEB-INF/classes下。
最后,新建文件: ...myApp/index.jsp
index.jsp
新建文件夹:...apache-tomcat-6.0.37\webapps\myApp
新建文件夹:...myApp\WEB-INF
新建文件夹:...WEB-INF/classes
新建文件: ...WEB-INF/web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <servlet> <servlet-name>Login</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping> </web-app>
LoginServlet.java
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { process(httpServletRequest,httpServletResponse); } @Override protected void doPost(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException{ process(httpServletRequest,httpServletResponse); } public void process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { String username = httpServletRequest.getParameter("username"); String password = httpServletRequest.getParameter("password"); httpServletResponse.setContentType("text/html"); PrintWriter printWriter = httpServletResponse.getWriter(); printWriter.println("<html><head><title>login</title></head>"); printWriter.println("<body>username: " + username + "<br>"); printWriter.println("password: " + password + "</body></html>"); } }
LoginServlet.java依赖于servlet-api.jar,可以在...apache-tomcat-6.0.37\lib下找到。
将LoginServlet.java编译后的LoginServlet.class放到WEB-INF/classes下。
最后,新建文件: ...myApp/index.jsp
index.jsp
<body> <form action="Login" method="post"> username: <input type="username" name="username"> <br> password: <input type="password" name="password"> <br> <input type="submit" value="submit"> <input type="reset" value="reset"> <br> </form> </body>
相关文章推荐
- Servlet+Tomcat制作出第一个运行在Tomcat上的Java应用程序
- Servlet+Tomcat制作出第一个运行在Tomcat上的java应用程序
- myeclipse+tomcat+servlet开发 运行第一个servlet程序 转载自冯永发(happyfa)的博客
- Eclipse运行Tomcat第一个小程序
- eclipse下运行servlet启动tomcat时提示端口被占用
- tomcat启动后让servlet一直运行
- MyEclipse第一个Servlet程序 --解决Win7系统下MyEclipse与Tomcat连接问题
- 怎样在tomcat5.0中怎样配置web.xml运行servlet
- Tomcat运行Servlet,JSP页面请求转发报错解决方法
- 在winXP在tomcat下配置运行jsp、servlet和ben的环境
- [转]MyEclipse第一个Servlet程序 --解决Win7系统下MyEclipse与Tomcat连接问题
- 运行servlet,构建并配置tomcat服务器的问题
- Tomcat下运行servlet
- 运行第一个简单servlet
- tomcat怎么运行servlet程序
- 怎样在tomcat5.0中怎样配置web.xml运行servlet
- Maven初识(四)——运行第一个Servlet,依赖范围讲解
- MyEclipse第一个Servlet程序 --解决Win7系统下MyEclipse与Tomcat连接问题
- Tomcat的安装与第一个servlet程序的部署
- [web开发] 如何在Mac OS上安装Apache Tomcat并运行你的第一个Servlet程序