如何在servlet中显示背景图像

时间:2016-05-25 06:48:43

标签: image servlets resources relative-path



XML



 上面你看到了我的Web-xml

这是我需要的输出,但是当与servlet集成时,我只能在没有图像 and this is output without servlet 的情况下登录



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ServletApplication</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>DemoServlet</display-name>
    <servlet-name>DemoServlet</servlet-name>
    <servlet-class>com.mayu.servlet.DemoServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DemoServlet</servlet-name>
    <url-pattern>/DemoServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>Login</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>com.mayu.servlet.Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>RoomMaster</display-name>
    <servlet-name>RoomMaster</servlet-name>
    <servlet-class>com.mayu.servlet.RoomMaster</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RoomMaster</servlet-name>
    <url-pattern>/RoomMaster</url-pattern>
  </servlet-mapping>
</web-app>
&#13;
 .hr {
            background: url("../img/login.jpg");
        }
&#13;
&#13;
&#13;

我正在开展一个处理<html> <head> <title>Reservation</title> <!-- Include CSS File Here --> <link rel="stylesheet" href="css/style.css" /> <link rel="stylesheet" href="css/Site.css" /> <link rel="stylesheet" href="css/bootstrap.css" /> <link rel="stylesheet" href="css/bootstrap.min.css" /> <!-- Include JS File Here --> <!--<script src="js/login.js"></script>--> <script src="js/jquery-1.9.0.1.min.js"></script> <script src="js/bootstrap.js"></script> </head> <body class="hr"> <form id="myform" method="post" action="RoomType.html" name="myform"> <div class="row"> <div class="col-md-8 col-sm-10 col-xs-12 col-md-offset-2 col-sm-offset-2"> <div class="jumbotron"> <form class="form-horizontal" style="margin-top:50px; margin-bottom:50px;"> <div class="form-group"> <label for="inputEmail3" class="col md-2 col-sm-2 col-md-offset-2 control-label" style="padding:0px;">User Id</label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="text" name="username" id="username" /> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-md-2 col-sm-2 col-md-offset-2 control-label" style="padding:0px;">Password</label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="password" name="password" id="password" /> </div> </div> <div class="form-group"> <div class="col-sm-offset-4 col-sm-3"> <!--<input type="button" value="Login" id="submit" onclick="myFunction()"/>--> <input type="button" onclick="myFunction()" value="Login"> </div> </div> </form> </div> </div> </div> </form> <script> function myFunction() { document.getElementById("myform").submit(); } $(document).ready(function () { $('.combobox').combobox(); //$('.combobox').combobox({newOptionsAllowed: false}); $('form').submit(function (e) { e.preventDefault(); alert($('input[name="normal"]').val()); alert($('input[name="horizontal"]').val()); alert($('input[name="inline"]').val()); }); }) //document.getElementById("TextBox1").value = Date() </script> </body> </html>的网络基础项目,我创建了servlet并将servlet pagecssjs添加到{ {1}}。我的问题是我将img folder设置为图像无法在浏览器上看到的正文背景。我添加了我的登录页面代码,请参阅并帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

尝试进行以下更改:

.hr {
        background-image: url("/img/login.jpg");
    }

答案 1 :(得分:0)

你使用错误的方式获取图像,文件的路径取决于你的战争名称,所以最好的方法是使用,所以如果你有这个结构:

Webapp
 |-- img
 |    `-- login.jpg
 `-- WEB-INF
      |-- 
      `--

您的图片网址为:'my_portlet_021_/img/login.jpg'并将此图片设置为背景我建议在jsp中使用jQuery:

var url='url(<%= request.getContextPath()%>/img/login.jpg)';
$('.hr').css("background-image", url);

此处<%= request.getContextPath()%>获取您的网络应用的名称。