- 浏览: 287067 次
- 性别:
- 来自: 龙城
最新评论
-
redey:
这垃圾东西,1.6以上JDK不支持
Jocky混淆JAVA代码(保护你的JAVA项目) -
u012907473:
水电费是否是否
js页面缓存的一个解决办法 -
jackson200:
讲解的很详细!
Jocky混淆JAVA代码(保护你的JAVA项目) -
jamesqq79:
下载解压缩后,不知是何文件格式,用PDF阅读器打不开。
Java程序员的推荐阅读书籍之十《Agile Java》 -
meimei727:
<!-- 给页面文件中的js和css引用增加版本号 -- ...
利用ant进行项目发布
原帖地址:http://blog.csdn.net/qhmao/archive/2008/10/19/3097753.aspx
在J2EE环境下使用JNDI是非常简单的事,因为所有的J2EE容器都要实现JNDI服务,所以,在J2EE环境下使用JNDI,与使用 Hashtable也没有什么太大区别。只有一点限制,那就是绑定对象时,对象所属的类必须实现java.io.Serializable接口,这一点也 实在一点也不困难,几乎所有用到的Java类都实现了这个接口,对于自定义的类,在接口实现列表里把这个接口加进去也就是了。
下面,我将演示一下如何在J2EE环境下使用JNDI,为了保证代码的通用性,我不使用struts之类的框架,而是直接使用标准JSP和Servlet实现。我将该项目的名称定为jndi_test
要使用JNDI,需要先到SUN的网站上去下载jndi.jar。
2.1 JSP
本项目包括5个JSP,功能说明如下:
- index.jsp:首页
- bind.jsp:用于在JNDI中绑定对象
- bind_result.jsp:绑定对象后的返回页面
- lookup.jsp:用于在JNDI中检索对象
- lookup_result.jsp:用于显示检索对象
本节中用到的JSP代码如下,代码都简单地很,就不多做解释了。
2.1.1 index.jsp
<%
...
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test </ title >
</ head >
< body >
< a href ="bind.jsp" target ="_blank" > bind.jsp </ a >
< br />
< a href ="lookup.jsp" target ="_blank" > lookup.jsp </ a >
</ body >
</ html >
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test </ title >
</ head >
< body >
< a href ="bind.jsp" target ="_blank" > bind.jsp </ a >
< br />
< a href ="lookup.jsp" target ="_blank" > lookup.jsp </ a >
</ body >
</ html >
2.1.2 bind.jsp
<%
...
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - Bind </ title >
</ head >
< body >
< a href ="bind.do" > bind an object </ a >
</ body >
</ html >
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - Bind </ title >
</ head >
< body >
< a href ="bind.do" > bind an object </ a >
</ body >
</ html >
2.1.3 bind_result.jsp
<%
...
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - Bind result </ title >
</ head >
< body >
< p > Binded successfully! </ p >
</ body >
</ html >
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - Bind result </ title >
</ head >
< body >
< p > Binded successfully! </ p >
</ body >
</ html >
2.1.4 lookup.jsp
<%
...
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - lookup </ title >
</ head >
< body >
< a href ="lookup.do" > lookup the binded object </ a >
</ body >
</ html >
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - lookup </ title >
</ head >
< body >
< a href ="lookup.do" > lookup the binded object </ a >
</ body >
</ html >
2.1.5 lookup_result.jsp
<%
...
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - Lookup result </ title >
</ head >
< body >
<% ...
Object o = request.getAttribute( " found_jndi_obj " );
out.println(o);
%>
</ body >
</ html >
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > JNDI Test - Lookup result </ title >
</ head >
< body >
<% ...
Object o = request.getAttribute( " found_jndi_obj " );
out.println(o);
%>
</ body >
</ html >
2.2 Servlet
本例包括两个Servlet,功能说明如下:
- BindServlet:用于在JNDI服务中绑定一个对象
- LookupServlet:用于在JNDI服务中取出一个对象
2.2.1 BindServlet.java
package
lld.test.jndi;
import java.io.IOException;
import java.util.Date;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http. * ;
public class BindServlet extends HttpServlet
... {
private static final long serialVersionUID = 5219969790998794367L ;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
this .doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
try
... {
Context jndi_ctx = new InitialContext();
String key = " jndi_object " ;
jndi_ctx.rebind(key, new Date());
} catch (Exception ex)
... {
ex.printStackTrace();
}
ServletContext context = this .getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher( " /bind_result.jsp " );
dispatcher.forward(req, resp);
}
}
import java.io.IOException;
import java.util.Date;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http. * ;
public class BindServlet extends HttpServlet
... {
private static final long serialVersionUID = 5219969790998794367L ;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
this .doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
try
... {
Context jndi_ctx = new InitialContext();
String key = " jndi_object " ;
jndi_ctx.rebind(key, new Date());
} catch (Exception ex)
... {
ex.printStackTrace();
}
ServletContext context = this .getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher( " /bind_result.jsp " );
dispatcher.forward(req, resp);
}
}
使用rebind而不是bind绑定对象是因为,使用bind时,如果已经有对象绑定到该键值上,则会抛出异常。
因为只是示例代码,所以我只是绑定了一个最简单的日期对象。
2.2.2 LookupServlet.java
package
lld.test.jndi;
import java.io.IOException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LookupServlet extends HttpServlet
... {
private static final long serialVersionUID = 6677219828267184673L ;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
this .doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
try
... {
Context jndi_ctx = new InitialContext();
String key = " jndi_object " ;
Object o = jndi_ctx.lookup(key);
req.setAttribute( " found_jndi_obj " , o);
} catch (Exception ex)
... {
ex.printStackTrace();
}
ServletContext context = this .getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher( " /lookup_result.jsp " );
dispatcher.forward(req, resp);
}
}
import java.io.IOException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LookupServlet extends HttpServlet
... {
private static final long serialVersionUID = 6677219828267184673L ;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
this .doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
... {
try
... {
Context jndi_ctx = new InitialContext();
String key = " jndi_object " ;
Object o = jndi_ctx.lookup(key);
req.setAttribute( " found_jndi_obj " , o);
} catch (Exception ex)
... {
ex.printStackTrace();
}
ServletContext context = this .getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher( " /lookup_result.jsp " );
dispatcher.forward(req, resp);
}
}
2.3 web.xml
在web.xml中,加入了servlet映射
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app id ="WebApp_ID" version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name > jndi_test </ display-name >
< servlet >
< servlet-name > BindServlet </ servlet-name >
< servlet-class > lld.test.jndi.BindServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > BindServlet </ servlet-name >
< url-pattern > /bind.do </ url-pattern >
</ servlet-mapping >
< servlet >
< servlet-name > LookupServlet </ servlet-name >
< servlet-class > lld.test.jndi.LookupServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > LookupServlet </ servlet-name >
< url-pattern > /lookup.do </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
< web-app id ="WebApp_ID" version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name > jndi_test </ display-name >
< servlet >
< servlet-name > BindServlet </ servlet-name >
< servlet-class > lld.test.jndi.BindServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > BindServlet </ servlet-name >
< url-pattern > /bind.do </ url-pattern >
</ servlet-mapping >
< servlet >
< servlet-name > LookupServlet </ servlet-name >
< servlet-class > lld.test.jndi.LookupServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > LookupServlet </ servlet-name >
< url-pattern > /lookup.do </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
OK,所有的代码都在这里了,部署到Tomcat下运行即可。
发表评论
-
autochk program not found 蓝屏重启问题解决
2012-04-17 10:54 13772起因: 因为硬盘空间不够,所以把原来的双系统中的ubu ... -
IOS开发一些资源
2012-02-06 16:07 1319从别的地方看到的,多谢作者,现贴在这里备忘。 在线教程 ... -
发现一个好东东,可以让浏览器跟本地桌面交互,哈哈
2011-09-08 17:35 1211http://gears.google.com/ -
nodejs开发运行环境搭建
2011-08-18 15:03 4346一. geddy 开发运行环境搭建 geddy是基 ... -
javascript来势凶猛
2011-08-15 17:22 1178引子 java编程弄了7,8个年头了,也 ... -
Oracle驱动包装
2011-07-06 17:06 1987见附件。 -
javaeye域名变了
2011-04-01 10:31 1250javaeye域名变了,才发现,哈哈,mark下。 -
拥抱敏捷
2011-01-15 17:27 1066前言 有关项目管理和软件开发方 ... -
重温设计模式
2011-01-13 10:27 1144策略模式: 定义了算 ... -
jsoup,html解析的利器
2011-01-07 09:21 1178http://jsoup.org/download -
关于html表格复制到excel
2010-09-09 14:04 5829刚才一个朋友问我这个事情,我拍脑袋想了一下,给他答复不可能,因 ... -
Java 路径 System.getProperty("key")的参数key
2010-09-02 14:37 1243java.version ... -
这种需求,大家看看有没有比较好的解决方案
2010-06-25 09:47 2239在我们的应用中,碰到了如图所示的一种网络结构。 重新描 ... -
java中singleton的几种实现方式
2010-06-24 15:08 1449传统的最简单的方式 这种模式有一个缺点就是不能实现延 ... -
oracle分页查询数据重复问题的解决
2010-06-24 11:00 3403在oracle分页查询中,我们采用类似以下所示的公认的比较高效 ... -
ubuntu10.04中安装使用IE6
2010-06-21 09:55 2546在用ubuntu910的时候,已经装了一遍IE了,但是升级到1 ... -
今天发现的两个有价值的东东
2010-06-17 15:45 1489其一,iRedMail,开源邮件解决方案。 其 ... -
ubuntu 10.04 中安装mysql5.1.4
2010-06-17 15:28 1802自从升级到10.04以后,mysql就不正常,卸载装了n次,均 ... -
升级到ubuntu 10.04,wine中的ie不正常了
2010-06-11 10:01 1679ubuntu上也折腾了半年了,日常工作生活基本没有太多的障碍了 ... -
升级到ubuntu10.04,mysql不能用了
2010-06-11 09:53 18509.10版本用了半年了,10.04发布了,看了10.04的宣传 ...
相关推荐
此文的目的是记录自己学习j2ee的一些过程。 这理解过程中可能有一些错误,如果高手看到了我的文章希望能够替我指正,感激不尽!!! 同时也希望能从一个初学者的角度去理解一些东西为后来学习j2ee的人提供一些...
JavaEE5学习笔记02-JNDI与RMI.
servlet学习笔记_JNDI.doc
NULL 博文链接:https://housheng33.iteye.com/blog/1522705
NULL 博文链接:https://hello-nick-xu.iteye.com/blog/2111215
Spring In Action(第二版)中文版_笔记 by uuwoxin 1.1 Spring是什么 如果说BeanFactory使Spring成为容器的话,那么上下文环境就是使Spring成为框架的原因。 上下文环境扩展了BeanFactory,添加了对I18N(国际化)...
Java与Java EE平台规范标准化了各种具体Java技术,比如,JDBC,JMX,Servlet,Annotation和JNDI API等,这些具体技术正是Spring2.0统一一体中的具体侧面。Spring2.0是Java和Java EE架构级框架,其依托的正是这些具体...
添加一些关于 jndi 的内容 3年前 java代码审计-sqli.md 第一的 4年前 java代码审计-ssrf.md 第一的 4年前 java代码审计-ssti.md 第一的 4年前 java代码审计-xss.md 第一的 4年前 java代码审计-xxe.md 第一的 4年前 ...
讲解j2ee中的每一项技术。jsp,servlet,jndi,ejb,jms,jta,rmi,xml....
JavaEE5学习笔记04-JavaMail使用总结----
j2ee学习参考(jsp,servlet,xml,ejb,security,JDBC,JTA/JTS,JCA,JNDI,Application Server)
我们所学习的均为jdbc2.0规范,jdbc1.0为直接连接,而jdbc2.0它有JNDI服务,在服务器启动时自动创建一个连接池,定义了一个DataSource接口,用其标准才能使用JDBC连接池
还包括两个反序列化有效负载生成器(在上可以看到:JRMPClient和CommonBeanutils1的JNDI变体)以及一个CVE-2018-2800的PoC: 直到2018年4月CPU(6u191、7u181、8u171),Java的RMI端点才允许对请求进行HTTP隧道传输...