java 如何在 NetBeans servlet/jsp 项目中使用 html 和/或 css 样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17760812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:54:01  来源:igfitidea点击:

How to use html and/or css styles in NetBeans servlet/jsp project?

javahtmlcssjspnetbeans

提问by nouser

I tried to create simple servlet project using NetBeans and I've started with login. Login page was done in html and it is stored same as css documment in NetBeans "Web pages" floder (web in directory structure).

我尝试使用 NetBeans 创建简单的 servlet 项目,并从登录开始。登录页面是在 html 中完成的,它与 NetBeans“Web pages” floder(目录结构中的 web)中的 css 文档相同。

Way of storing files in NetBeans

在 NetBeans 中存储文件的方式

If I try it to view page, all works fine with css included. However if I try to run this file ( shift + F6 ), there is error in browser that says something about fail of redirecting. Error during opening html page

如果我尝试查看页面,则在包含 css 的情况下一切正常。但是,如果我尝试运行此文件( shift + F6 ),浏览器中会出现错误,说明重定向失败。 打开html页面时出错

Similar fail is there, if I try to use css file in jsp page. Although I have all html text in jsp page fine and it works if I use plain html page, in jsp page css doesn't work. To solve this problem I tried to use two servers, Glassfish and Apache Tomcat, both are not working. I've just also tried to check several topics that are similar, but description for including css to jsp wasn't working. Next task, that I have there is redirection by response.sendRedirect() function to login page. If I try to redirect to jsp page, it works fine (without css included), but if I try to redirect to html, it doesn't works with same error as is shown above. Same fail happens with not valid address stored as sendRedirect() function parameter. According some answers of this server I have stored html and css file in correct path (please consider the first image in this question).

如果我尝试在 jsp 页面中使用 css 文件,则会出现类似的失败。虽然我在 jsp 页面中的所有 html 文本都很好,如果我使用纯 html 页面它可以工作,但在 jsp 页面中 css 不起作用。为了解决这个问题,我尝试使用两台服务器,Glassfish 和 Apache Tomcat,都无法正常工作。我刚刚也尝试检查几个相似的主题,但是将 css 包含到 jsp 的描述不起作用。下一个任务,我有通过 response.sendRedirect() 函数重定向到登录页面。如果我尝试重定向到 jsp 页面,它可以正常工作(不包含 css),但是如果我尝试重定向到 html,它不会出现与上面显示的相同的错误。存储为 sendRedirect() 函数参数的无效地址也会发生同样的失败。

Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource

引用放置在 JSP 文件中的 WEB-INF 文件夹中的资源会在资源上返回 HTTP 404

I think, that web servers simply doesn't know apropriate address for the web page and the css file. Is there some way to add it to web.xml or somewhere else to view the html page? If there is some another failure, how can I fix it? How can I add some external css file to jsp page?

我认为,Web 服务器根本不知道网页和 css 文件的适当地址。有没有办法将它添加到 web.xml 或其他地方来查看 html 页面?如果还有其他故障,我该如何修复?如何向jsp页面添加一些外部css文件?

Great thanks for reply!

非常感谢回复!

After ArtyMcFly response added: Thanks for reply. I tried all that you described in your answer. my web.xml looks like that now:

ArtyMcFly 回复后补充:感谢回复。我尝试了您在回答中描述的所有内容。我的 web.xml 现在看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

 //This is changed code from MartyMcFly   
    <servlet>
    <servlet-name>errorPage</servlet-name>
    <jsp-file>/WEB-INF/errorPage.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>errorPage</servlet-name>
    <url-pattern>/errorPage</url-pattern>
</servlet-mapping>


    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>Controler.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

and I also changed link in servlet. to

我还更改了 servlet 中的链接。到

<link rel="stylesheet" type="text/css" href="./css/main.css" />

My css file is stored under Web Pages/css/main.css now. but there is nothing works.

我的 css 文件现在存储在 Web Pages/css/main.css 下。但没有任何效果。

I cannot add screenshots until i will have 10 points. This is some server policy...

在我有 10 分之前,我无法添加屏幕截图。这是一些服务器策略...

PS: Because I have now over 10 points, I've just added some print screen explaining situation.

PS:因为我现在已经超过10分,所以我只是添加了一些打印屏幕来解释情况。

回答by Arthur Weborg

EDITremoved initial response as it did not help the situation, the following is a working-set-up of css and jsp, both in the Deployment Descriptor(web.xml) and the file structure

编辑删除了初始响应,因为它对情况没有帮助,以下是部署描述符(web.xml)和文件结构中的 css 和 jsp 的工作设置

I looked at one of my netbean set-ups and my structure looks like the following:

我查看了我的一个 netbean 设置,我的结构如下所示:

/WEB-INF/jsp/*all my jsp pages*
/css/*css pages*

the Deployment descriptor (web.xml) looks like the following:

部署描述符 (web.xml) 如下所示:

<servlet>
    <servlet-name>jsppage1</servlet-name>
    <jsp-file>/WEB-INF/jsp/jsppage1.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>jsppage1</servlet-name>
    <url-pattern>/jsppage1</url-pattern>
</servlet-mapping>

and I included the css within the jsp with the following code:

我使用以下代码在jsp中包含了css:

<link rel="stylesheet" type="text/css" href=" css/main.css" />

So what's going on in the code is it is mapping the URL address /jsppage1 to the file in the WEB-INF folder, and as far as the jsp file is concerned it's being stored then at the root of the project, even though it's in the file structure for the WEB-INF.

那么代码中发生的事情是它将 URL 地址 /jsppage1 映射到 WEB-INF 文件夹中的文件,并且就 jsp 文件而言,它被存储在项目的根目录中,即使它在WEB-INF 的文件结构。