如何在Tomcat服务器8.0.2上部署和运行第一个JSP应用程序?

时间:2015-05-30 03:32:05

标签: java mysql jsp tomcat

我希望一位雄猫老将能在这里帮助我!我有两个主要问题(链接页面和数据库连接)

  

一个。链接页面:

  1. 我将myapps.war重命名为ROOT.war;

  2. 我在tomcat/web-apps/application.com/ROOT部署了WAR文件 然后Tomcat解压缩WAR并生成一个子文件;

  3. 我已将jstl.jar放入tomcat/web-apps/application.com/ROOT/WEB-INF/lib

  4. index.jsp在浏览器中显示良好。一切似乎都很好但是当我点击任何给定的网址时,/viewCartcart.jsp还是category?{category.id}来显示所选类别的数据,我会在浏览器中收到404 Http error resource not available 。所有页面(索引页面除外)都存储在tomcat/web-apps/application.com/ROOT/WEB-INF/view

    由于除了index.jsp之外没有其他页面显示,我猜我必须将文件移动并将它们放在index.jsp的同一子目录中?但它是否不会删除WEB-INF的所有目的来隐藏动态页面?

    我有点困惑。我不知道我做错了什么......

      

    B中。数据库连接

    1. 我已下载平台独立的JDBC驱动程序here并将其放置(mysql-connector-java-version-bin.jarin tomcat/web-apps/application.com/ROOT/WEB-INF/lib
    2. 我正在尝试遵循此文档:http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-tomcat.html

      这有点令人困惑......在文档中他们说通过在定义Web应用程序的上下文中向$ CATALINA_HOME / conf / server.xml添加声明资源来配置JNDI DataSource,并且它们提供了一段代码。我对此细分的问题是:server.xml的含义是什么?它们是web.xml还是persistence.xml?因为我的应用程序用来与数据库交互的数据源的JNDI名称可以在持久性单元(persistence.xml文件)中找到并存储在tomcat/web-apps/application.com/ROOT/WEB-INF/classes/META-INF中...我应该将persistence.xml移到别处或者我应该在web.xml

      中添加一段代码

      编辑请求 - 配置文件

      的persistence.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
        <persistence-unit name="ApplicationNamePU" transaction-type="JTA">
        <jta-data-source>jdbc/applicationName</jta-data-source>
        <properties>
          <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>
      </persistence-unit>
      </persistence>
      

      SERVER.XML - 未经修改

      <?xml version='1.0' encoding='utf-8'?>
      <!--
        Licensed to the Apache Software Foundation (ASF) under one or more
        contributor license agreements.  See the NOTICE file distributed with
        this work for additional information regarding copyright ownership.
        The ASF licenses this file to You under the Apache License, Version 2.0
        (the "License"); you may not use this file except in compliance with
        the License.  You may obtain a copy of the License at
      
            http://www.apache.org/licenses/LICENSE-2.0
      
        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
      -->
      <!-- Note:  A "Server" is not itself a "Container", so you may not
           define subcomponents such as "Valves" at this level.
           Documentation at /docs/config/server.html
       -->
      <Server port="15005" shutdown="SHUTDOWN" address="127.8.97.129">
        <!-- Security listener. Documentation at /docs/config/listeners.html
        <Listener className="org.apache.catalina.security.SecurityListener" />
        -->
        <!--APR library loader. Documentation at /docs/apr.html -->
        <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
        <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
        <Listener className="org.apache.catalina.core.JasperListener" />
        <!-- Prevent memory leaks due to use of particular java/javax APIs-->
        <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
        <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
        <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
      
        <!-- Global JNDI resources
             Documentation at /docs/jndi-resources-howto.html
        -->
        <GlobalNamingResources>
          <!-- Editable user database that can also be used by
               UserDatabaseRealm to authenticate users
          -->
          <Resource name="UserDatabase" auth="Container"
                    type="org.apache.catalina.UserDatabase"
                    description="User database that can be updated and saved"
                    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                    pathname="conf/tomcat-users.xml" />
        </GlobalNamingResources>
      
        <!-- A "Service" is a collection of one or more "Connectors" that share
             a single "Container" Note:  A "Service" is not itself a "Container",
             so you may not define subcomponents such as "Valves" at this level.
             Documentation at /docs/config/service.html
         -->
        <Service name="Catalina">
      
          <!--The connectors can use a shared executor, you can define one or more named thread pools-->
          <!--
          <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
              maxThreads="150" minSpareThreads="4"/>
          -->
      
      
          <!-- A "Connector" represents an endpoint by which requests are received
               and responses are returned. Documentation at :
               Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
               Java AJP  Connector: /docs/config/ajp.html
               APR (HTTP/AJP) Connector: /docs/apr.html
               Define a non-SSL HTTP/1.1 Connector on port 8080
          -->
          <Connector port="8080" protocol="HTTP/1.1"
                     connectionTimeout="20000" address="127.8.97.129" 
                     redirectPort="8443" />
          <!-- A "Connector" using the shared thread pool-->
          <!--
          <Connector executor="tomcatThreadPool"
                     port="8080" protocol="HTTP/1.1"
                     connectionTimeout="20000"
                     redirectPort="8443" />
          -->
          <!-- Define a SSL HTTP/1.1 Connector on port 8443
               This connector uses the JSSE configuration, when using APR, the
               connector should be using the OpenSSL style configuration
               described in the APR documentation -->
          <!--
          <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
                     maxThreads="150" scheme="https" secure="true"
                     clientAuth="false" sslProtocol="TLS" />
          -->
      
          <!-- Define an AJP 1.3 Connector on port 8009 -->
          <Connector port="15009" protocol="AJP/1.3" redirectPort="8443" address="127.8.97.129" />
      
      
          <!-- An Engine represents the entry point (within Catalina) that processes
               every request.  The Engine implementation for Tomcat stand alone
               analyzes the HTTP headers included with the request, and passes them
               on to the appropriate Host (virtual host).
               Documentation at /docs/config/engine.html -->
      
          <!-- You should set jvmRoute to support load-balancing via AJP ie :
          <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
          -->
          <Engine name="Catalina" defaultHost="localhost">
      
            <!--For clustering, please take a look at documentation at:
                /docs/cluster-howto.html  (simple how to)
                /docs/config/cluster.html (reference documentation) -->
            <!--
            <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
            -->
      
            <!-- Use the LockOutRealm to prevent attempts to guess user passwords
                 via a brute-force attack -->
            <Realm className="org.apache.catalina.realm.LockOutRealm">
              <!-- This Realm uses the UserDatabase configured in the global JNDI
                   resources under the key "UserDatabase".  Any edits
                   that are performed against this UserDatabase are immediately
                   available for use by the Realm.  -->
              <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                     resourceName="UserDatabase"/>
            </Realm>
      
            <Host name="localhost"  appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
      
              <!-- SingleSignOn valve, share authentication between web applications
                   Documentation at: /docs/config/valve.html -->
              <!--
              <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
              -->
      
              <!-- Access log processes all example.
                   Documentation at: /docs/config/valve.html
                   Note: The pattern used is equivalent to using pattern="common" -->
              <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                     prefix="localhost_access_log" suffix=".txt"
                     pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      
            </Host>
          </Engine>
        </Service>
      </Server>
      

      3RD EDIT ON REQUEST

      / cart的控制器条目:

      // if cart page is requested
              } else if (userPath.equals("/viewCart")) {
      
                  String clear = request.getParameter("clear");
      
                  if ((clear != null) && clear.equals("true")) {
      
                      ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
                      cart.clear();
                  }
      
                  userPath = "/cart";
      
      
      // if addToCart action is called
              if (userPath.equals("/addToCart")) {
      
                  // if user is adding item to cart for first time
                  // create cart object and attach it to user session
                  if (cart == null) {
      
                      cart = new ShoppingCart();
                      session.setAttribute("cart", cart);
                  }
      
                  // get user input from request
                  String productId = request.getParameter("productId");
      
                  if (!productId.isEmpty()) {
      
                      Product product = productFacade.find(Integer.parseInt(productId));
                      cart.addItem(product);
                  }
      
                  userPath = "/category";
      
      
              // if updateCart action is called
              } else if (userPath.equals("/updateCart")) {
      
                  // get input from request
                  String productId = request.getParameter("productId");
                  String quantity = request.getParameter("quantity");
      
                  boolean invalidEntry = validator.validateQuantity(productId, quantity);
      
                  if (!invalidEntry) {
      
                      Product product = productFacade.find(Integer.parseInt(productId));
                      cart.update(product, quantity);
                  }
      
                  userPath = "/cart";
      

1 个答案:

答案 0 :(得分:1)

回答你的问题:

一个。链接页面

您的网址如何链接到jsp网页取决于您使用的网络框架。您使用的是Spring MVC还是Struts?

例如,如果使用Spring MVC,则链接由控制器中的@RequestMapping完成,并查看xml配置中定义的解析器。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

如果使用struts 2,则链接在struts.xml文件和操作中定义。示例:http://struts.apache.org/docs/hello-world-using-struts-2.html

B中。数据库连接

当文档说$ CATALINA_HOME / conf / server.xml时,它指的是你的apache tomcat实例主目录。转到您的Tomcat文件夹(类似于apache-tomcat-7.0.42),您将看到一个名为conf的文件夹,其中包含所有tomcat配置文件(如server.xml,context.xml,web.xml,...) 。您可以在那里指定数据库连接。但请记住,如果您这样做,部署到此tomcat的所有Web应用程序将共享相同的配置。我使用JNDI指定数据库连接的首选方法是将其放在META-INF文件夹下的context.xml文件中。通过这种方式,我可以为我的应用程序提供特定的数据库配置。