初次使用servlet写HelloWorld
来源:互联网 发布:信息工程研究所 知乎 编辑:程序博客网 时间:2024/06/06 19:09
自己是一名学习软件工程的大学生,在大二上学期,本人第一次接触JSP,第一堂课就是用servlet写了一个HelloWorld
首先,我用的是Myeclipse集成开发工具,先创建了一个web工程即web project,然后建一个叫Text01的包
然后在建一个叫Text01的java类,在建立类的时候注意,点击Browser,然后输入httpservlet,选择javax.servlet包下的httpservlet
我的结构目录如下:
Text01.java
package Text01;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Text01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setCharacterEncoding("UTF-8");//设置请求的字符集类型 resp.setContentType("text/html;charset=utf-8");//设置响应的字符集类型 PrintWriter out=resp.getWriter();//设置输出流 out.println("Hello,我要学习JSP了,请多多关照");//打印一段话在网页上}}
在web工程下的webroot文件下的WEB-INF下的web.xml进行配置:
<?xml version="1.0" encoding="UTF-8"?><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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>Text01.Text01</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping></web-app>
然后部署tomcat,右键tomcat,有个add ,把工程添加进去即可,然后debug
然后在浏览器输入:localhost:8080/工程名/映射的url地址即url-pattern里面的hello
按照我的情况,我要在浏览器输入:http://localhost:8080/Text/hello
然后我就可以访问自己写的servlet了!
注:写完servlet后,一定要在web,xml进行配置,配置的格式是先写一个servlet标签,然后在servlet标签里写上name和class
class写包名.类名,比如我的包是Text01,类名是Text01,我就在servlet-class标签里写Text01.Text01
可以按住ctrl,用鼠标点击该class,如果能点进去,就说明配置成功
第一次发微博,以后会不断更新技术帖
1 0
- 初次使用servlet写HelloWorld
- 使用Strust2框架写HelloWorld
- 使用Strust2框架写HelloWorld
- 使用Strust2框架写HelloWorld
- ok210使用-初次烧写程序
- Servlet Helloworld
- MAC下用MyEclipse写一个servlet的helloworld
- 初次使用servlet实现对数据的增删改查
- 第一次使用hibernate写的helloworld
- VS2017使用方法 使用C语言写helloworld
- 初次使用vue写项目的一些经验
- 初次使用Construct2写鼠标操作型射击游戏
- 初次使用
- 初次使用
- 初次使用
- 初次使用
- 初次使用
- 初次使用
- .NET pfx文件解析私钥和公钥
- 从上往下打印二叉树
- COBOL call C function
- 单链表基础
- Java-003-朝花夕拾
- 初次使用servlet写HelloWorld
- nginx反向代理和rewrite进行解决跨域问题、去掉url中的一部分字符串,通过nginx正则生成新的url
- COBOL PERFORM verb
- iOS库-库的基本常识
- HDU 1542 Atlantis 【线段树+扫描线】
- springmvc的进阶
- http://www.uisdc.com/front-end-designer-cooperation
- 自定义对话框
- java程序运行机制和jvm