为什么Firefox打开我的JSP网页文件作为原始源代码而不显示实际页面?

时间:2011-08-21 22:36:53

标签: jsp firefox

我正在运行Java Pet Store示例,这是我的“index.jsp”文件的顶部:

<%-- Copyright 2006 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html
$Id: index.jsp,v 1.20 2007-03-16 20:18:59 basler Exp $ --%>

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.util.*, com.sun.javaee.blueprints.petstore.model.CatalogFacade, com.sun.javaee.blueprints.petstore.model.Tag"%>

<%
try {
    CatalogFacade cf = (CatalogFacade)config.getServletContext().getAttribute("CatalogFacade");
    List<Tag> tags=cf.getTagsInChunk(0, 12);
    // since top 20 come from database or desending refCount order, need to reorder by tag name
    Collections.sort(tags, new Comparator() {
        public int compare(Object one, Object two) {
             int cc=((Tag)two).getTag().compareTo(((Tag)one).getTag());
             return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
        }
    });    
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
        <title>Java Pet Store Reference Application</title>
        <link type="text/css" rel="stylesheet" href="./tag.css"/>
    </head>
    <body>

...

3 个答案:

答案 0 :(得分:6)

可能没有为服务JSP页面正确配置服务器,或者可能没有为JSP页面正确配置mime / type。

答案 1 :(得分:2)

这不是你问题的直接答案,但我会尝试展示如何渲染JSP。遵循该路径,并对domains / domain1 / server.log进行分析(如果您的安装使用名为domain1的默认域),可能会提示您有什么问题。

通常在GF2中,域的config目录中有一个名为default-web.xml的文件。

它包含以下代码段:

  <servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
      <param-name>xpoweredBy</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
  </servlet>

上面的代码配置了JSP支持 - 当然如果它找不到servlet代码就会失败,但是应该记录在server.log中。

之后,以下片段确保调用上述servlet来管理jsp - 一个用于“经典”jsp的映射和一个用于“xml语法”jsps的映射。

  <servlet-mapping>
     <servlet-name>jsp</servlet-name>
     <url-pattern>*.jspx</url-pattern>
  </servlet-mapping>


  <!-- ================ Built In Servlet Mappings ========================= -->


  <!-- The mapping for the JSP servlet -->
  <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

现在,如果出于某种原因,default-web.xml中缺少这些段,jsp支持将处于非活动状态,并且您将获得类似于您描述的效果,因为默认的servlet将拾取该文件,并将作为静态资源处理。

答案 2 :(得分:0)

这可能已经很老但我也遇到过这个问题,它与使用浏览器显示JSP页面有关(此页面不是由服务器托管或管理的,换句话说服务器是关)。

所以这是对所发生情况的图解说明:

有一个加载firefox的页面只显示原始代码/计划文本 img1

正确加载了相同的页面,出于验证目的,其源代码可见,注意两个页面具有相同的代码 img2

发生此问题是因为两个页面都有不同的文件包含,一个是ANSI(正常显示的那个),另一个是UTF-8(显示原始代码的那个)。所以要解决这个问题,我们需要:

  1. 用记事本打开有问题的页面
  2. 文件&gt;另存为..
  3. 在编码组合框中选择ANSI
  4. 选择保存类型组合框
  5. 中的所有文件
  6. 再次打开页面以验证是否正确显示
  7. img3

    页面正确显示 img4