当前位置:首页 > 网络编程 > WEB编程 > JSP > 常见问题F&Q(新手推荐)

常见问题F&Q(新手推荐)

点击次数:28 次 发布日期:2008-11-26 16:26:44 作者:源代码网
源代码网推荐
  如何混合使用Jsp和SSI #include?

  在JSP中可以使用如下方式包含?HTML:

  <!--#include file="data.inc"-->

  但是如果data.inc中包含JSP CODE ,我?可以使用:

  <%@include file="data.inc"%>

  如何?行一??程安全的JSP?

  只需增加如下指令

  <%@ page isThreadSafe="false" %>

  JSP如何?理HTML FORM中的?料?

  通??置的request物件即可,如下:

  <%

  String item = request.getParameter("item");

  int howMany = new Integer(request.getParameter("units")).intvalue();

  %>

  在JSP如何包含一???文件?

  ??包含如下:<%@ include file="copyright.html" %>

  ??包含如下:<jsp:include page="copyright.html" flush="true"/>

  在JSP中如何使用注??

  主要有四中方法:

  1。<%-- ? --%>

  2。//

  3。/**?**/

  4。<!--?-->

  在JSP中如何?行??重定向?

  使用如下方式即可:response.sendRedirect("http://ybwen.home.chinaren.com/index.html";);

  也能物理地改?HTTP HEADER?性,如下:

  <%

  response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);

  String newLocn="/newpath/index.html";

  response.setHeader("Location",newLocn);

  %>

  如何防止在JSP或SERVLET中的?出不被BROWSER保存在CACHE中?

  把如下?本加入到JSP文件的?始即可:

  <%

  response.setHeader("Cache-Control","no-store"); file://HTTP 1.1

  response.setHeader("Pragma","no-cache"); file://HTTP 1.0

  response.setDateHeader ("Expires", 0); file://prevents caching at the proxy server

  %>

  在JSP中如何?置COOKIE?

  COOKIE是作?HTTP HEADER的一部分被?送的,如下方法即可?置:

  <%

  Cookie mycookie = new Cookie("aName","avalue");

  response.addCookie(mycookie);

  %>

  在JSP中如何?除一?COOKIE?

  <%

  Cookie killMyCookie = new Cookie("mycookie", null);

  killMyCookie.setMaxAge(0);

  killMyCookie.setPath("/");

  response.addCookie(killMyCookie);

  %>

  在一?JSP的?求?理中如何停止JSP的?行?

  如下例:

  <%

  if (request.getParameter("wen") != null) {

  // do something

  } else {

  return;

  }

  %>

  在JSP中如何定?方法?

  你可以定?方法,但是你不能直接??JSP的?置物件,而是通???的方法??。如下:

  <%!

  public String howBadFrom(HttpServletRequest req) {

  HttpSession ses = req.getSession();

  ...

  return req.getRemoteHost();

  }

  %>

  <%

  out.print("in general,lao lee is not baddie ");

  %>

  <%= howBadFrom(request) %>

  如果BROWSER已??了COOKIES,在JSP中我如何打?SESSION?跟??

  使用URL重?即可,如下:

  hello1.jsp

  <%@ page session="true" %>

  <%

  Integer num = new Integer(100);

  session.putvalue("num",num);

  String url =response.encodeURL("hello2.jsp");

  %>

  hello2.jsp</a>

  hello2.jsp

  <%@ page session="true" %>

  <%

  Integer i= (Integer )session.getvalue("num");

  out.println("Num value in session is "+i.intvalue());

  %>

  在JSP中能?送EMAIL??

  可以使用SUN的?用包:sun.net.smtp包。如下?本使用SmtpClient??送EMAIL。

  <%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>

  <%

  String from="ybwen@sina.com";

  String to="hewenjun@yeah.net, lei@who.com.cn";

  try{

  SmtpClient client = new SmtpClient("mail.xxxxx.xxx");

  client.from(from);

  client.to(to);

  PrintStream message = client.startMessage();

  message.println("To: " + to);

  message.println("Subject: Sending email from JSP!");

  message.println("This was sent from a JSP page!");

  message.println();

  message.println("Cool! :-)");

  message.println();

  message.println("Good Boy");

  message.println("I"m in genius.com");

  message.println();

  client.closeServer();

  }

  catch (IOException e){

  System.out.println("ERROR SENDING EMAIL:"+e);

  }

  %>

  在SERVLET中我能?用一?JSP?????

  ?然???,如下展示了如何在一?SERVLET控制???元??用一?JSP???面。

  protected void sendErrorRedirect(HttpServletRequest request,

  HttpServletResponse response, String errorPageURL,

  Throwable e)

  throws ServletException, IOException {

  request.setAttribute ("javax.servlet.jsp.jspException", e);

  getServletConfig().getServletContext().

  getRequestDispatcher(errorPageURL).forward(request,

  response);

  }

  public void doPost(HttpServletRequest request,HttpServletResponse response) {

  try {

  // do something

  } catch (Exception ex) {

  try {

  sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  JSP如何?EJB SessionBean通??

  下面的代?段作了很好的示?

  <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject,

  foo.AccountHome, foo.Account" %>

  <%!

  file://定?一??SessionBeanHome介面?例的全局引用

  AccountHome accHome=null;

  public void jspInit() {

  file://?得Home介面?例

  InitialContext cntxt = new InitialContext( );

  Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB");

  accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class);

  }

  %>

  <%

  file://?例化SessionBean

  Account acct = accHome.create();

  file://?用?端方法

  acct.doWhatever(...);

  // 如此等等

  %>

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华