最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

JSP中可能会碰到的问题解答

日期:08-02    来源:中国设计秀    作者:cnwebshow.com

jsp中可能会碰到的问题解答  c9V中国设计秀
1、如何混合使用Jsp和SSI #include? c9V中国设计秀
在JSP中可以使用如下方式包含纯HTML: c9V中国设计秀
&lt !--#include file="data.inc"-- &gt c9V中国设计秀
但是如果data.inc中包含JSP CODE ,我们可以使用: c9V中国设计秀
&lt <A href="%@includemailto:%@include">%@include</A> file="data.inc"% &gt </P>c9V中国设计秀

 c9V中国设计秀

2、如何执行一个线程安全的JSP? c9V中国设计秀
只需增加如下指令 c9V中国设计秀
&lt %@ page isThreadSafe="false" % &gt </P>c9V中国设计秀

 c9V中国设计秀

3、JSP如何处理HTML FORM中的数据? c9V中国设计秀
通过内置的request对象即可,如下: c9V中国设计秀
&lt % c9V中国设计秀
String item = request.getParameter("item"); c9V中国设计秀
int howMany = new Integer(request.getParameter("units")).intvalue(); c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

4、在JSP如何包含一个静态文件? c9V中国设计秀
静态包含如下:&lt %@ include file="copyright.html" % &gt c9V中国设计秀
动态包含如下:&lt jsp:include page="copyright.html" flush="true"/ &gt </P>c9V中国设计秀

 c9V中国设计秀

5、在JSP中如何使用注释? c9V中国设计秀
主要有四中方法: c9V中国设计秀
1。&lt %-- 与 --% &gt c9V中国设计秀
2。// c9V中国设计秀
3。/**与**/ c9V中国设计秀
4。&lt !--与-- &gt </P>c9V中国设计秀

 c9V中国设计秀

6、在JSP中如何执行浏览重定向? c9V中国设计秀
使用如下方式即可:response.sendRedirect("<A href="http://ybwen.home.chinaren.com/index.htmlhttp://ybwen.home.chinaren.com/index.html">http://ybwen.home.chinaren.com/index.html</A>"); c9V中国设计秀
也能物理地改变HTTP HEADER属性,如下: c9V中国设计秀
&lt % c9V中国设计秀
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); c9V中国设计秀
String newLocn="/newpath/index.html"; c9V中国设计秀
response.setHeader("Location",newLocn); c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

7、如何防止在JSP或SERVLET中的输出不被BROWSER保存在CACHE中? c9V中国设计秀
把如下脚本加入到JSP文件的开始即可: c9V中国设计秀
&lt % c9V中国设计秀
response.setHeader("Cache-Control","no-store"); //HTTP 1.1 c9V中国设计秀
response.setHeader("PRagma","no-cache"); //HTTP 1.0 c9V中国设计秀
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

8、在JSP中如何设置COOKIE? c9V中国设计秀
COOKIE是作为HTTP HEADER的一部分被发送的,如下方法即可设置: c9V中国设计秀
&lt % c9V中国设计秀
Cookie mycookie = new Cookie("aName","avalue"); c9V中国设计秀
response.addCookie(mycookie); c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

9、在JSP中如何删除一个COOKIE? c9V中国设计秀
&lt % c9V中国设计秀
Cookie killMyCookie = new Cookie("mycookie", null); c9V中国设计秀
killMyCookie.setMaxAge(0); c9V中国设计秀
killMyCookie.setPath("/"); c9V中国设计秀
response.addCookie(killMyCookie); c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

10、在一个JSP的请求处理中如何停止JSP的执行 c9V中国设计秀
如下例: c9V中国设计秀
&lt % c9V中国设计秀
if (request.getParameter("wen") != null) { c9V中国设计秀
// do something c9V中国设计秀
} else { c9V中国设计秀
return; c9V中国设计秀
} c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

11、在JSP中如何定义方法 c9V中国设计秀
你可以定义方法,但是你不能直接访问JSP的内置对象,而是通过参数的方法传递。如下: c9V中国设计秀
&lt %! c9V中国设计秀
public String howBadFrom(HttpServletRequest req) { c9V中国设计秀
Httpsession ses = req.getSession(); c9V中国设计秀
... c9V中国设计秀
return req.getRemoteHost(); c9V中国设计秀
} c9V中国设计秀
% &gt c9V中国设计秀
&lt % c9V中国设计秀
out.print("in general,lao lee is not baddie "); c9V中国设计秀
% &gt c9V中国设计秀
&lt %= howBadFrom(request) % &gt </P>c9V中国设计秀

 c9V中国设计秀

12、如果BROWSER已关闭了COOKIES,在JSP中我如何打开SESSION来跟踪 c9V中国设计秀
使用URL重写即可,如下: c9V中国设计秀
hello1.jsp c9V中国设计秀
&lt %@ page session="true" % &gt c9V中国设计秀
&lt % c9V中国设计秀
Integer num = new Integer(100); c9V中国设计秀
session.putvalue("num",num); c9V中国设计秀
String url =response.encodeURL("hello2.jsp"); c9V中国设计秀
% &gt c9V中国设计秀
&lt a href=&lt %=url% &gt &gthello2.jsp&lt /a &gt </P>c9V中国设计秀

c9V中国设计秀
hello2.jsp c9V中国设计秀
&lt %@ page session="true" % &gt c9V中国设计秀
&lt % c9V中国设计秀
Integer i= (Integer )session.getvalue("num"); c9V中国设计秀
out.println("Num value in session is "+i.intvalue()); c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

13、在JSP中能发送EMAIL吗 c9V中国设计秀
可以使用SUN的专用包:sun.net.smtp包。如下脚本使用SmtpClient类发送EMAIL。 c9V中国设计秀
&lt %@ page import="sun.net.smtp.SmtpClient, java.io.*" % &gt c9V中国设计秀
&lt % c9V中国设计秀
String from="<A href="ybwen@sina.commailto:ybwen@sina.com">ybwen@sina.com</A>"; c9V中国设计秀
String to="<A href="hewenjun@yeah.netmailto:hewenjun@yeah.net">hewenjun@yeah.net</A>, <A href="lei@who.com.cnmailto:lei@who.com.cn">lei@who.com.cn</A>"; c9V中国设计秀
try{ c9V中国设计秀
SmtpClient client = new SmtpClient("mail.xxxxx.xxx"); c9V中国设计秀
client.from(from); c9V中国设计秀
client.to(to); c9V中国设计秀
PrintStream message = client.startMessage(); c9V中国设计秀
message.println("To: " + to); c9V中国设计秀
message.println("Subject: Sending email from JSP!"); c9V中国设计秀
message.println("This was sent from a JSP page!"); c9V中国设计秀
message.println(); c9V中国设计秀
message.println("Cool! :-)"); c9V中国设计秀
message.println(); c9V中国设计秀
message.println("Good Boy"); c9V中国设计秀
message.println("Im in genius.com"); c9V中国设计秀
message.println(); c9V中国设计秀
client.closeServer(); c9V中国设计秀
} c9V中国设计秀
catch (IOException e){ c9V中国设计秀
System.out.println("ERROR SENDING EMAIL:"+e); c9V中国设计秀
} c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

14、在SERVLET中我能调用一个JSP错误页吗 c9V中国设计秀
当然没问题,如下展示了如何在一个SERVLET控制逻辑单元内调用一个JSP错误页面。 c9V中国设计秀
protected void sendErrorRedirect(HttpServletRequest request, c9V中国设计秀
HttpServletResponse response, String errorPageURL, c9V中国设计秀
Throwable e) c9V中国设计秀
throws ServletException, IOException { c9V中国设计秀
request.setAttribute ("javax.servlet.jsp.jspException", e); c9V中国设计秀
getServletConfig().getServletContext(). c9V中国设计秀
getRequestDispatcher(errorPageURL).forward(request, c9V中国设计秀
response); c9V中国设计秀
} </P>c9V中国设计秀

c9V中国设计秀
public void doPost(HttpServletRequest request,HttpServletResponse response) { c9V中国设计秀
try { c9V中国设计秀
// do something c9V中国设计秀
} catch (Exception ex) { c9V中国设计秀
try { c9V中国设计秀
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex); c9V中国设计秀
} catch (Exception e) { c9V中国设计秀
e.printStackTrace(); c9V中国设计秀
} c9V中国设计秀
} c9V中国设计秀
} </P>c9V中国设计秀

 c9V中国设计秀

15、JSP和APPLET如何通讯 c9V中国设计秀
JSP如何与EJB SessionBean通讯 c9V中国设计秀
下面的代码段作了很好的示范 c9V中国设计秀
&lt %@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, c9V中国设计秀
foo.AccountHome, foo.Account" % &gt c9V中国设计秀
&lt %! c9V中国设计秀
//定义一个对SessionBeanHome接口实例的全局引用 c9V中国设计秀
AccountHome accHome=null; </P>c9V中国设计秀

c9V中国设计秀
public void jspInit() { c9V中国设计秀
//获得Home接口实例 c9V中国设计秀
InitialContext cntxt = new InitialContext( ); c9V中国设计秀
Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB"); c9V中国设计秀
accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class); c9V中国设计秀
} c9V中国设计秀
% &gt c9V中国设计秀
&lt % c9V中国设计秀
//实例化SessionBean c9V中国设计秀
Account acct = accHome.create(); c9V中国设计秀
//调用远程方法 c9V中国设计秀
acct.doWhatever(...); c9V中国设计秀
// 如此等等 c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

16、当我使用一个结果集时,如何防止字段为"null"的字域显示在我的HTML输入文本域中? c9V中国设计秀
可以定义一个简单的函数来达到目的,如下: c9V中国设计秀
&lt %! c9V中国设计秀
String blanknull(String s) { c9V中国设计秀
return (s == null) ? "" : s; c9V中国设计秀
} c9V中国设计秀
% &gt </P>c9V中国设计秀

c9V中国设计秀
然后在JSP的FORM中,可以这样使用 c9V中国设计秀
&lt input type="text" name="shoesize" value="&lt %=blanknull(shoesize)% &gt" &gt </P>c9V中国设计秀

 c9V中国设计秀

17、如何中SERVLET或JSP下载一个文件(如:binary,text,executable)? c9V中国设计秀
现提供两个解决方案: c9V中国设计秀
A:使用HTTP, c9V中国设计秀
B:在Servlet中,通过设置ContentType和使用java.io包的Stream等类可作到.例如: c9V中国设计秀
response.setContentType("application/x-msword"); c9V中国设计秀
然后想输出缓冲中写一些东东即可。 </P>c9V中国设计秀

c9V中国设计秀
18、使用useBean标志初始化BEAN时如何接受初始化参数 c9V中国设计秀
使用如下两标签即可: c9V中国设计秀
&lt jsp:getProperty name="wenBean" property="someProperty"/ &gt c9V中国设计秀
&lt jsp:setProperty name="wenBean" property="someProperty" value="somevalue"/ &gt </P>c9V中国设计秀

c9V中国设计秀
19、使用JSP如何获得客户浏览器的信息? c9V中国设计秀
使用request.getHeader(String)即可 </P>c9V中国设计秀

 c9V中国设计秀

20、能象调用子程序一样调用JSP吗? c9V中国设计秀
当然可以,用&lt jsp:include page="relativeURL" flush="true"/ &gt </P>c9V中国设计秀

 c9V中国设计秀

21、当我重编译我的JSP使用的一个类后,为什么JVM继续使用我的老CLASS? </P>c9V中国设计秀

 c9V中国设计秀

&lt <A href="%@includemailto:%@include">%@include</A> file="abc.jsp"% &gt与&lt jsp:include page="abc.jsp"/ &gt之间的差别? c9V中国设计秀
前一个为静态包含,而后一个为动态包含 </P>c9V中国设计秀

 c9V中国设计秀

22、JSP的缺点? c9V中国设计秀
1。对JAVA程序进行调试没有好东东 c9V中国设计秀
2。因大多数的servlet引擎不支持connection pooling c9V中国设计秀
3。Servlet引擎没有标准 c9V中国设计秀
4。JSP与其它脚本语言的交互 </P>c9V中国设计秀

 c9V中国设计秀

23、JSP能进行递归调用吗? c9V中国设计秀
当然可以,如对form的提交给本页 </P>c9V中国设计秀

 c9V中国设计秀

34、如何实现JSP的国际化? c9V中国设计秀
为各种版本提供resource bundles属性文件即可 </P>c9V中国设计秀

c9V中国设计秀
25、在JSP中如何写文本文件? c9V中国设计秀
使用PrintWriter对象,如: c9V中国设计秀
&lt %@ page import="java.io.*" % &gt c9V中国设计秀
&lt % c9V中国设计秀
String str = "print me"; c9V中国设计秀
String nameOfTextFile = "/usr/anil/imp.txt"; c9V中国设计秀
try { c9V中国设计秀
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile)); c9V中国设计秀
pw.println(str); c9V中国设计秀
pw.close(); c9V中国设计秀
} catch(IOException e) { c9V中国设计秀
out.println(e.getMessage()); c9V中国设计秀
} c9V中国设计秀
% &gt </P>c9V中国设计秀

 c9V中国设计秀

26、如何在JSP中包括绝对路径文件? c9V中国设计秀
使用URLConnection即可。 </P>c9V中国设计秀

 c9V中国设计秀

27、在servlets和JSP之间能共享session对象吗? c9V中国设计秀
当然可以, c9V中国设计秀
HttpSession session = request.getSession(true); c9V中国设计秀
session.putvalue("variable","value"); </P>c9V中国设计秀

 c9V中国设计秀

28、Javascript的变量能复制到JSP的SESSION中吗? </P>c9V中国设计秀

 c9V中国设计秀

29、如何设置cookie在某一时间后过期? c9V中国设计秀
用Cookie.setMaxAge(int) </P>c9V中国设计秀

 c9V中国设计秀

30、如何获得当前的sessions数? c9V中国设计秀
可以使用HttpSessionBindingListeners来跟踪 </P>c9V中国设计秀

 c9V中国设计秀

31、能设置一些代码在我所有的JSP文件之上运行?如果可以,能共享吗? c9V中国设计秀
当然可以,可以为你的JSP文件定义一个别名:/jsp/=ybwen.genius.myPreprocessingServlet,而以/jsp/为前缀的文件可以使用 </P>c9V中国设计秀

 c9V中国设计秀

32、对一个JSP页,如果多个客户端同时请求它,同步可能吗? c9V中国设计秀
在jsp:useBean语法中使用beanName有何好处? c9V中国设计秀
beanName使用Beans.instantiate()初始化Bean </P>c9V中国设计秀

 c9V中国设计秀

33、当我使用&lt jsp:forward &gt时,在浏览器的地址栏没有改变? c9V中国设计秀
使用response.sendRedirect("newURL") </P>c9V中国设计秀

 c9V中国设计秀

34、如何转换JSP 0.9版本的文件到JSP1.1? c9V中国设计秀
可使用sed/awk即可 </P>c9V中国设计秀

 c9V中国设计秀

35、使用JSP能设置HTML FORM中输入域的焦点,不用Javascript? c9V中国设计秀
没办法 </P>c9V中国设计秀

 c9V中国设计秀

36、使用JSP连接到数据库连接缓冲池的最好方法是什么? c9V中国设计秀
1。使用JDBC2。0中带有此服务的Driver c9V中国设计秀
2.使用提供有此服务的Application server c9V中国设计秀

本文引用地址:/bc/article_46639.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明