JavaWeb学习笔记——开发动态WEB资源(二)HelloWord
该工程的功能是在页面上输出一段话
首先在src里面新建一个class,在interface里面添加javax.servlet.Servlet
以下是HelloServlet.java中的代码:
package org.common.Servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class HelloServlet implements Servlet { @Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
} @Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
} @Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub } //开发完成之后要告诉服务器我的存在,就要在web。xml里面继续写代码
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
//获取一个输出流,就可以对页面写出内容
PrintWriter pw = arg1.getWriter();
pw.println("终于成功了!");
pw.close();
} }
以下是web.xml文件中的代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 定义Servlet服务 -->
<servlet>
<servlet-name>hello</servlet-name> <!--随便取-->
<servlet-class>org.common.Servlet.HelloServlet</servlet-class> <!--取包名。类名-->
</servlet> <!-- 定义映射关系 -->
<servlet-mapping>
<!-- 注意:mapping里面的servlet-name在Servlet的定义里面一定要有匹配的,否则启动报错 -->
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
访问的网址是http://127.0.0.1:8080/Helloapp/hello,其中Helloapp是Tomcat里面的文件夹的名字,hello是url-pattern里面写的名字
JavaWeb学习笔记——开发动态WEB资源(二)HelloWord的更多相关文章
- JavaWeb学习笔记——开发动态WEB资源(一)Java程序向浏览器输出数据
开发一个动态web资源,即开发一个Java程序向浏览器输出数据,需要完成以下2个步骤: 1.编写一个Java类,实现Servlet接口 开发一个动态web资源必须实现javax.servlet.Ser ...
- JavaWeb学习笔记——开发动态WEB资源(六)ServletConfig和ServletContext
1.只有在第一次请求服务器产生实例的时候才会调用init()方法,有一种办法能在服务器一启动的时候就加载init()方法. 即服务器启动即加载Servlet,且按数字大小顺序实例化Servlet. 方 ...
- JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证
本工程的功能是实现Javaweb的servlet身份验证 一下是login.html文件中的代码 <!DOCTYPE html> <html> <head> < ...
- JavaWeb学习笔记——开发动态WEB资源(八)cookies和httpsession
会话: cookies: (1)cookies是WEB服务器发送到浏览器的简短文本信息 (2)cookies可以禁用 httpsession: 一次会话是从你打开浏览器开始到你关闭浏览器结束 提供一种 ...
- JavaWeb学习笔记——开发动态WEB资源(七)bookapp
该工程的功能是实现一个bookapp 1.开发注册页面,注册使用properties文件,存储在classpath跟路径 2.注册成功跳转到登录页面 3.输入用户名密码登录,登录成功跳转到book显示 ...
- JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法
该工程的名称是testhttp,功能是在页面中表格打印浏览过程中的相关头信息. 新建一个工程,然后在这个工程里面新建一个servlet,这样便可以省去编写web.xml的过程 以下是TestHttpS ...
- JavaWeb学习笔记——开发动态WEB资源(三)显示当前时间
该工程的功能是实现在页面中显示当前的时间 以下的代码是HelloServlet.java中的代码 package helloapp2; import java.io.IOException; impo ...
- Ruby学习笔记4: 动态web app的建立
Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...
- Ruby学习笔记6: 动态web app的建立(3)--多Model之间的交互
We first built a static site which displayed a static image using only a Controller and a View. This ...
随机推荐
- 0929mysql 用户管理和权限设置
转来自http://www.cnblogs.com/fslnet/p/3143344.html 用户管理 mysql>use mysql; 查看 mysql> select host,us ...
- Qt学习中遇到的问题
问题: 一个Qt小项目,编译成功并成功运行,但应用程序输出中出现如下异常:FTH: (9892): *** Fault tolerant heap shim applied to current pr ...
- [转]session 持久化问题(重启服务器session 仍然存在)
转:http://xiaolongfeixiang.iteye.com/blog/560800 关于在线人数统计,大都使用SessionListener监听器实现. SessionListener 触 ...
- 100200H
这是个bfs 首先建图,先从终点bfs求出每点距离,然后从起点开始,确定初始方向:某点和自己相邻距离比自己小1就是 然后就先贪心和上次一样的方向,如果不能走,就找出一个方向,把自己当前方向改掉,重复过 ...
- spring-从普通java类取得注入spring Ioc容器的对象的方案
1.启动服务时通过spring容器的监听器(继承ContextLoaderListener 监听器的方法) public class ListenerSpringContext extends Con ...
- hibernate-取消关联外键引用数据丢失抛异常的设置@NotFound
hibernate项目里面配了很多many-to-one的关联,后台在查询数据时已经作了健全性判断,但还是经常抛出对象找不到异常: org.hibernate.ObjectNotFoundExcept ...
- How to fix the sources list
How to fix the sources list Sometimes the apt-get may not work, it is often caused by the misspelled ...
- Urtext的使用
UITextField 是UIControl的子类,UIControl又是UIView的子类,所以也是一个视图,只不过比UIView多了两个功能,1.文字显示,2.文本编辑 使用过程分四步: 1.创建 ...
- 和ef一起使用的一些知识点。
ObjectContext.ExecuteFunction 方法 (String, ObjectParameter[]) .NET Framework 4.6 and 4.5 执行在数据源中定义 ...
- codevs1183泥泞的道路
题意:给定一张有向稠密图和通过每条边的时间和路程,问从1到n的路程/时间 最大为多少 正解:SPFA+二分答案 开始做的时候,想直接跑图论,后来发现好像不对(不然数据范围怎么这么小) 但是显然要用到图 ...