Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rogueai/2321281189da3585adfdfdecf8b9077b to your computer and use it in GitHub Desktop.
Save rogueai/2321281189da3585adfdfdecf8b9077b to your computer and use it in GitHub Desktop.
A servlet that receives data from an AS400 RPG program
package com.academy.wms.footwearsorter;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class StartResourcesServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger LOGGER = LoggerFactory.getLogger(StartResourcesServlet.class);
DataSource dataSource;
public void init() throws ServletException {
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
dataSource = (DataSource)envContext.lookup("jdbc/testdb");
} catch (NamingException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LOGGER.debug("servlet calling!");
try {
Connection conn = dataSource.getConnection();
CallableStatement cs=null;
ResultSet rs=null;
String query = "call DEVDPGH/SQLRPGJDBC(?,?)";
List<String> results = new ArrayList<String>();
try {
cs = conn.prepareCall(query);
cs.setString(1, "Academy Sports ");
cs.setString(2, "+ Outdoors");
rs = cs.executeQuery();
rs.next();
String firstResult = rs.getString("FIRSTFIELD");
String lastResult = rs.getString("LASTFIELD");
request.getSession().setAttribute("line1", firstResult);
request.getSession().setAttribute("line2", lastResult);
} catch(SQLException e){
e.printStackTrace();
} finally {
try { if(null!=rs)rs.close();} catch (SQLException e)
{e.printStackTrace();}
try { if(null!=cs)cs.close();} catch (SQLException e)
{e.printStackTrace();}
try { if(null!=conn)conn.close();} catch (SQLException e)
{e.printStackTrace();}
}
} catch(Exception e) {
System.out.println( e.getMessage() );
}
RequestDispatcher dispatcher = request.getRequestDispatcher("helloworld.jsp");
dispatcher.forward(request, response);
}
}
<?xml version="1.0" ?>
<project name="footwearsorter" default="war">
<property file="build.properties"/>
<property name="src.dir" value="src"/>
<property name="web.src" value="src/WebContent"/>
<property name="build.dir" value="bin"/>
<property name="dist.dir" value="dist"/>
<path id="compile.classpath">
<fileset dir="${web.src}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${appserver.home}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${dist.dir}" />
<delete dir="${build.dir}" />
</target>
<target name="init" depends="clean">
<fail>
<condition>
<not>
<resourcecount count="1">
<fileset dir="src/main/resources" includes="log4j.properties"/>
</resourcecount>
</not>
</condition>
</fail>
<mkdir dir="${build.dir}/classes"/>
<mkdir dir="${dist.dir}" />
</target>
<target name="compile" depends="init" >
<javac destdir="${build.dir}/classes" includeantruntime="false" debug="true" srcdir="${src.dir}/main/java">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="${dist.dir}/${ant.project.name}.war" webxml="${web.src}/WEB-INF/web.xml">
<lib dir="${web.src}/WEB-INF/lib"/>
<fileset dir="${web.src}/"><include name="**/*.*"/></fileset>
<classes dir="${src.dir}/main/resources"><include name="*.*"/></classes>
<classes dir="${build.dir}/classes"/>
</war>
</target>
<target name="deploywar" depends="war" description="Deploy application as a WAR file">
<copy todir="${appserver.home}/webapps" preservelastmodified="true">
<fileset dir="${dist.dir}">
<include name="*.war"/>
</fileset>
</copy>
</target>
</project>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>hello</title>
</head>
<body>
<br/><br/>
${line1} ${line2}
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>hello</title>
</head>
<body>
<br/><br/>
<form id="form1" name="form1" action="start" method="POST">
<br/><br/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
H OPTION(*SRCSTMT:*NODEBUGIO) DFTACTGRP(*NO)
d sds
d pgmnam 1 10
d usrprf 254 263
d systim 282 287 0
d rpgjdbc PR ExtPgm('RPGJDBC')
d parm1 15
d parm2 15
d rpgjdbc PI
d parm1 15
d parm2 15
d capitalize PR 15
d parm1 15
D Results DS qualified dim(1)
d firstField 15
d lastField 15
d N s 3P 0 inz(1)
/free
Results(1).firstField = capitalize(parm1);
Results(1).lastField = capitalize(parm2);
exec sql set result sets for return to client
array :Results for :N rows;
*INLR = *ON;
/end-free
p capitalize b EXPORT
d pi 15A
d strToCap 15A
d up C 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
d lo C 'abcdefghijklmnopqrstuvwxyz'
d rtnval s 15
C eval rtnval = %xlate(lo:up:strToCap)
C return rtnval
p e
-- run this on AS400 with command RUNSQLSTM
-- DROP PROCEDURE DEVDPGH/SQLRPGJDBC
CREATE PROCEDURE DEVDPGH/SQLRPGJDBC (IN CHAR(15), IN CHAR (15))
DYNAMIC RESULT SETS 1
LANGUAGE RPGLE
READS SQL DATA
EXTERNAL NAME 'DEVDPGH/RPGJDBC'
PARAMETER STYLE GENERAL ;
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>footwearsorter</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
<servlet>
<servlet-name>startServlet</servlet-name>
<servlet-class>com.academy.wms.footwearsorter.StartResourcesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>startServlet</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment