Aufruf eines Servlets auf einer JSP Seite funktioniert nicht

Andyx1975

Mitglied
ich habe ein Problem mit einer JSP Seite. Ich bin zurzeit dabei eine Website mit JSP, EclipseLink und Servlets zu basteln. Ich habe jetzt das Problem, dass wenn ich die JSP über den Link "http://127.0.0.1:8888/news.jsp" aufrufe, bekomme ich den Wert NULL zurück geliefert. Wenn ich das Servlet über den Browser aufrufe mit dem Link "http://127.0.0.1:8888/websiteNews" funktioniert es. Weiß jemand wo mein Problem liegt? Das Servlet funktioniert ja wohl offensichtlich. Mache ich beim Aufruf in der JSP was falsch?

Der Aufruf des Servlets in der JSP sieht wie folgt aus:
Code:
<!-- AUFRUF DES SERVLETS -->
				<%
					out.print(request.getAttribute("news"));
				%>

Meine web.xml beinhaltet folgendes:
Code:
<servlet>
     	<servlet-name>NewsForWebsite</servlet-name>
     	<servlet-class>com.uds.webadmin.server.CServletWebsiteNews</servlet-class>
	</servlet>
	<servlet-mapping>
     	<servlet-name>NewsForWebsite</servlet-name>
     	<url-pattern>/websiteNews</url-pattern>
	</servlet-mapping>

Das Servlet sieht wie folgt aus:
Code:
package com.uds.webadmin.server;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import com.uds.webadmin.data.DNews;
import com.uds.webadmin.data.DSettings;
import com.uds.webadmin.data.DUpload;

import javax.servlet.*;

import javax.servlet.http.*;

public class CServletWebsiteNews
    extends HttpServlet
{
    @PersistenceContext( unitName = "persistance", type = PersistenceContextType.TRANSACTION )
    private static String PERSISTENCE_UNIT_NAME = "LSF-5434"; // Name of the persistance unit which will be chosen
    private static EntityManagerFactory factory = Persistence.createEntityManagerFactory( PERSISTENCE_UNIT_NAME );

    public void doGet( HttpServletRequest request, HttpServletResponse response )

        throws ServletException, IOException
    {
        response.setContentType( "text/html" );
        PrintWriter out = response.getWriter();
        String newsItem = "";
        File file = null;

        // Search for a central upload path
        CRestSettings prop = new CRestSettings();
        DSettings settings = prop.getAllSettings();
        String path = settings.getUploadPath();
        if ( path.equals( "" ) )
        {
            // gets the file name / path
            file = new File( DUpload.class.getResource( "/" ).getPath() );
            file = file.getParentFile();
            file = file.getParentFile();
            file =
                new File( file.getAbsoluteFile() + System.getProperty( "file.separator" ) + "uploads"
                    + System.getProperty( "file.separator" ) );
        }
        else
        {
            file =
                new File( path + System.getProperty( "file.separator" ) + "uploads"
                    + System.getProperty( "file.separator" ) );
        }

        try
        {
            EntityManager em = factory.createEntityManager();
            Query query = em.createQuery( "SELECT e FROM DNews e ORDER BY e.news_id DESC" );
            List<DNews> news = query.getResultList();
            em.close();

            for ( int i = 0; i < news.size(); i++ )
            {

                String picture = "";
                DUpload upload = news.get( i ).getPicture();
                if ( upload != null )
                {
                    picture = file.getName() + "/" + upload.getUpload_link();
                }
                String headline = news.get( i ).getNews_headline();
                String text = news.get( i ).getTxt();

                newsItem =
                    newsItem + "<article>" + "<img src=\" " + picture + "\" alt=\"\" class=\"thumb\" /> "
                        + "<p><a href=\"#\" class=\"title\">" + headline + "</a></p><p class=\"story\">" + text
                        + "</p>" + "</article>";
            }

        }
        catch ( Exception e )
        {

            System.out.println( "Exception is ;" + e );

        }

        request.setAttribute( "news", newsItem );

        // Disptching request
        RequestDispatcher dispatcher = request.getRequestDispatcher( "news.jsp" );

        if ( dispatcher != null )
        {
            dispatcher.forward( request, response );
        }

    }

}

Die JSP sieht wie folgt aus:
Code:
<!doctype html>
<%@page language="java" import="java.util.*" %>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>XXXX</title>
	<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
	<link rel="stylesheet" type="text/css" href="print.css" media="print" />
	<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
	<script type="text/javascript" src="js/simpletabs.js"></script> 
	<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>
<div id="wrapper"><!-- #wrapper -->

	<header><!-- header -->
		<h1><a href="#">XXXXXXX</a></h1> <!-- main title on the left side-->
		<h2>XXXXXXXXX</h2> <!-- subtitle on the right side -->
		<img src="images/headerPicture.jpg" width="940" height="200" alt=""><!-- header image -->
	</header><!-- end of header -->
	
	<nav><!-- top nav -->
		<div class="menu">
			<ul>
    			<li><a href="contact.jsp">Contact</a></li>
    			<li><a href="about.jsp">About Us</a></li>
    			<li><a href="bands.jsp">Bands</a></li>
    			<li><a href="tours.jsp">Tours</a></li>
    			<li><a href="news.jsp">News</a></li>
			</ul>
		</div>
	</nav><!-- end of top nav -->
	
	<section id="main"><!-- #main content and sidebar area -->
			<section id="content"><!-- #content -->
				
				
				<!--article class="featured">
					<img src="images/newspic.jpg" alt="" class="featuredthumb" />
					<p><a href="#" class="featuredtitle">This is the featured article</a></p><p class="featuredstory">Pellentesque ut sapien arcu, egestas mollis massa. Fusce lectus leo, fringilla at aliquet sit amet, volutpat non erat. Aenean molestie nibh vitae turpis venenatis vel accumsan nunc tincidunt. Pellentesque ut sapien arcu, egestas mollis massa. Fusce lectus leo, fringilla at aliquet sit amet, volutpat non erat. Aenean molestie nibh vitae turpis venenatis vel accumsan nunc tincidunt. Pellentesque ut sapien arcu, egestas mollis massa. Fusce lectus leo, fringilla at aliquet.</p>
				</article>
				
				

				<article>
					<img src="images/newspic1.jpg" alt="" class="thumb" />
					<p><a href="#" class="title">This is the title of a news article</a></p><p class="story">Pellentesque ut sapien arcu, egestas mollis massa. Fusce lectus leo, fringilla at aliquet sit amet, volutpat non erat. Aenean molestie nibh vitae turpis venenatis vel accumsan nunc tincidunt. Pellentesque ut sapien arcu, egestas mollis massa.</p>
				</article-->
				
				
				<!-- AUFRUF DES SERVLETS -->
				<%
					out.print(request.getAttribute("news"));
				%>

			</section><!-- end of #content -->

		<aside id="sidebar"><!-- sidebar -->

			<div class="tabwidget"> 
  	  <ul class="tabs"> 
  	      <li><a href="#tab1">Recent</a></li> 
  	      <li><a href="#tab2">Popular</a></li>
  	      <li><a href="#tab3">About Me</a></li>
  	  </ul> 
  	  <div class="tab_container"> 
  	      <div id="tab1" class="tab_content"> 
  	          <h3>Recent Articles</h3> 
  	          <ul>
  	          	<li><a href="#">Interesting Recent Link 1</a></li>
			<li><a href="#">Interesting Recent Link 2</a></li>
			<li><a href="#">Interesting Recent Link 3</a></li>
			<li><a href="#">Interesting Recent Link 4</a></li>
			<li><a href="#">Interesting Recent Link 5</a></li>
			<li><a href="#">Interesting Recent Link 6</a></li>
			<li><a href="#">Interesting Recent Link 7</a></li>
							</ul>
  	      </div> 
  	      <div id="tab2" class="tab_content"> 
  	          <h3>Our Most Popular</h3> 
  	          <ul>
  	          	<li><a href="#">Most Popular Link 1</a></li>
			<li><a href="#">Most Popular Link 2</a></li>
			<li><a href="#">Most Popular Link 3</a></li>
			<li><a href="#">Most Popular Link 4</a></li>
		</ul>
  	      </div> 
  	      <div id="tab3" class="tab_content"> 
  	          <h3>Who I Am</h3> 
  	          <img src="images/me.png" alt="" /><p>Lorem ipsum in libris debitis facilis qui, in usu esse falli deseruisse. Vel ut vivendo sensibus partiendo, liber philosophia ne vim. Viris oblique explicari has et. Nam noster iisque discere te, sale doming delectus est te, an debet quando eruditi pro. Ad dicant praesent persequeris has, ne vel minimum petentium periculis.</p>
  	      </div>
  	     </div>
  	    </div>
  	    
  	     <div class="standard">
        	
				<h3>Connect With Us</h3>
					<ul>
						<li><a href="#">Twitter</a></li>
						<li><a href="#">Facebook</a></li>
						<li><a href="#">LinkedIn</a></li>
						<li><a href="#">Flickr</a></li>
					</ul>
			</div>

		</aside><!-- end of sidebar -->

	</section><!-- end of #main content and sidebar-->
	
		<footer>
		<section id="footer-area">

			<section id="footer-outer-block">
					<aside class="footer-segment">
							<h4>XXXXXXXXXXXXXXXXX</h4>
								<p>&copy; 2012 <a href="#">Impressum</a></p>
					</aside><!-- end of #fourth footer segment -->

			</section><!-- end of footer-outer-block -->

		</section><!-- end of footer-area -->
	</footer>
	
</div><!-- #wrapper -->
</body>
</html>
 

Neue Beiträge