1.1.7. fejezet, Jetty konfigurálás

Ajánlás

Elsősorban kis memóriaigényű, egyszerű servlet containerek építésére alkalmazzák (Google why choose jetty vs. tomcat).

Modulok

startup

JNDI

${jetty.home}/modules/jndi.mod:

#
# JNDI Support
#
[xml]
etc/jetty-jndi.xml
 
[depend]
server
 
[lib]
lib/jetty-jndi-${jetty.version}.jar
lib/jndi/*.jar

${jetty.home}/etc/jetty-jndi.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New class="org.eclipse.jetty.plus.jndi.EnvEntry">
	<Arg></Arg>
	<Arg>mySpecialValue</Arg>
	<Arg type="java.lang.Integer">5000</Arg>
	<Arg type="boolean">true</Arg>
</New>
</Configure>

start.ini:

...
--module=plus
 
--module=jndi

DataSource

forrás

DataSource jetty.xml-ben:

<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
   <Arg></Arg>
   <Arg>jdbc/DSTest</Arg>
   <Arg>
       <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">          
          <Set name="url">jdbc:mysql://localhost:3306/jdbcauthtest?useUnicode=true&amp;autoReconnect=true&amp;characterEncoding=UTF-8</Set>
          <Set name="User">test</Set>
          <Set name="Password">test</Set>
       </New>
   </Arg>
</New>

Hivatkozás a web.xml-ben:

	<resource-ref>
		<description>My DataSource Reference</description>
		<res-ref-name>jdbc/DSTest</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

Adatbázis elérése Java-ban:

			Hashtable props = new Hashtable(3);
			props.put("java.naming.factory.url.pkgs","org.eclipse.jetty.jndi");
			props.put("java.naming.factory.initial","org.eclipse.jetty.jndi.InitialContextFactory");
			InitialContext  ic = new InitialContext(props);
			DataSource myDS = (DataSource)ic.lookup("java:comp/env/jdbc/DSTest");
			Connection conn = myDS.getConnection();
			Statement stmt = conn.createStatement();
			stmt.execute("select * from SystemUser");
			ResultSet rs = stmt.getResultSet();
			response.setCharacterEncoding("UTF-8");
			response.getWriter().append("<html>"
					+ "<meta charset=\"UTF-8\">"
					+ "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />"
					+ "<body><table border='1'><tbody>");
			while(rs.next()) {
				response.getWriter().append("<tr>");
				String userName = rs.getString("username");
				String emailAddress = rs.getString("emailAddress");
				response.getWriter().append("<td>"+userName+"</td><td>"+emailAddress+"</td>");
				response.getWriter().append("</tr>");
			}
			response.getWriter().append("</tbody></table></body></html>");
CsatolmányMéret
Csomag ikon Homework4DynamicJetty.war_.zip1.15 MB