adalm pluto_Apache Pluto&Perl集成教程示例

adalm pluto

Perl is a family of high level language that adapted willingly with the Web as it’s used a Text-based processing. For such that integration we used the Portlet Perl bridge with some modifications that we already achieved on the source code that’s provided by Apache SVN for that Bridge.

Perl是一门高级语言家族,因为它使用了基于文本的处理,因此很容易适应Web。 为了实现这种集成,我们使用Portlet Perl桥进行了一些修改,这些修改已经在Apache SVN为该桥提供的源代码上完成。

Perl integration isn’t an easy mission as you may face a lot of issues raised once you’re getting the default library of Apache Perl Bridge used. The direct use of Perl bridge without any modifications will throw a ClassCastException as the bridge expects that your RenderResponse instance that’s passed is of type HttpServletResponseWrapper which is actually not.

与Perl集成并不是一件容易的事,因为一旦使用了默认的Apache Perl Bridge库,您可能会遇到很多问题。 直接使用Perl桥而不进行任何修改将抛出ClassCastException,因为桥希望传递的RenderResponse实例的类型为HttpServletResponseWrapper ,但实际上不是。

It seems that’s working nicely when it comes to use it within Portlet Container Specification 1.0. Regardless of that, we are now here; Portlet Container Specification 2.0 and we must provide you a library for doing that.

在Portlet容器规范1.0中使用它时,似乎工作得很好。 无论如何,我们现在在这里; Portlet容器规范2.0,我们必须为您提供一个执行此操作的库。

However, you won’t be working on the Bridge modifications as it’s already modified and ready for your use. Instead, you will get the latest JAR downloadable here and it will be installed into your Maven repository just like any library for being referenced then from your Pom file.

但是,您将无法进行Bridge修改,因为已经对其进行了修改并可以使用。 相反,您可以从此处下载最新的JAR,并将其安装到Maven存储库中,就像从Pom文件中引用任何库一样。

This tutorial intended to provide you the same sample of Employee Registration that’s already covered in a lot of Tutorials we’ve provided before. As the integration of Perl is so complicated mission, so we will focus on the way in which you may get that done instead of focusing on the way in which you may use Perl effectively.

本教程旨在为您提供与我们之前提供的许多教程中已经涵盖的相同的员工注册示例。 由于Perl的集成是一项非常复杂的任务,因此,我们将专注于完成任务的方式,而不是专注于有效使用Perl的方式。

项目目录结构 (Project Directory Structure)

员工表 (Employee Table)

As being of our suggested business model is just a process of employee registration, it’s very trivial to know that we have one Table called employee and it looks like below:

由于我们建议的业务模型只是员工注册的过程,因此知道一个名为employee的表非常简单,如下所示:

Employee Table

As you can create that Table into your database using the below SQL statement:


您可以使用以下SQL语句将该表创建到数据库中:

Employee.sql

Employee.sql

CREATE TABLE `employee` (
  `EMP_ID` int(11) NOT NULL AUTO_INCREMENT,
  `EMP_NAME` varchar(45) DEFAULT NULL,
  `EMP_JOB` varchar(45) DEFAULT NULL,
  `EMP_SALARY` int(11) DEFAULT NULL,
  PRIMARY KEY (`EMP_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

员工模式 (Employee Model)

According for MVC design pattern, the data that’s should be served must be located in a data model. For employee registration process, Employee data model should look like below:

根据MVC设计模式,应提供的数据必须位于数据模型中。 对于员工注册过程,员工数据模型应如下所示:

Employee.java

Employee.java

package com.journaldev.data;

public class Employee {
	// Employee Identification
	private int id;
	// Employee Name
	private String name;
	// Employee Job
	private String job;
	// Employee Salary
	private int salary;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}
}

下载Apache Perl Bridge (Download Apache Perl Bridge)

Multiple choices are introduced for you to get install Apache Perl bridge library; you can reference it directly from the Maven repository, fetch the source code from the Apache source code control or download JournalDev Release directly.

为安装Apache Perl桥库引入了多种选择。 您可以从Maven 存储库直接引用它,从Apache源代码控件中获取源代码,或直接下载JournalDev Release

We’ve preferred you to download JournalDev release as it’s being modified to prevent you from getting the mentioned exception above.

我们希望您下载JournalDev版本,因为它已被修改,以防止您遇到上述异常。

Referencing Apache Perl library from Maven repository should make it available into your local repository and so no need for further Maven commands execution. In case you chose to fetch Apache Perl from the source code control, then you need to execute mvn clean install to get a ready JAR upon pom.xml file that’s located on the fetched folder.

从Maven存储库引用Apache Perl库应该使它可用于本地存储库,因此不需要进一步执行Maven命令。 如果您选择从源代码控制中获取Apache Perl,则需要执行mvn clean install来获取位于获取的文件夹上的pom.xml文件上的就绪JAR。

But in case you’re downloading JournalDev release you must follow the below steps to make sure you’re able of referencing the library from your project’s maven file:

但是,如果要下载JournalDev版本,则必须遵循以下步骤,以确保能够从项目的maven文件中引用该库:

  • Save downloaded ZIP file into your local directory.

    将下载的ZIP文件保存到本地目录中。
  • Change file extension from ZIP into JAR.

    将文件扩展名从ZIP更改为JAR。
  • Navigate into your download JAR location through using of command line and execute mvn install:install-file -DgroupId=org.apache.portals.bridges -DartifactId=portals-bridges-perl -Dversion=1.0.4 -Dpackaging=jar -Dfile={PATH-TO-FILE}.

    通过使用命令行导航到您的下载JAR位置,然后执行mvn install:install-file -DgroupId=org.apache.portals.bridges -DartifactId=portals-bridges-perl -Dversion=1.0.4 -Dpackaging=jar -Dfile={PATH-TO-FILE}
  • Make sure that you were finished Maven installation successfully.

    确保您已成功完成Maven安装。

Maven构建文件 (Maven Build File)

Once you finished previous library installation, let’s glance the maven build file required:

完成先前的库安装后,让我们看一下所需的maven构建文件:

pom.xml

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.journaldev</groupId>
	<artifactId>PerlBridge</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>PerlBridge</name>
	<url>https://maven.apache.org</url>
	<properties>
		<deployFolder>D:/ApachePluto/pluto-2.0.3/webapps</deployFolder>
	</properties>
	<dependencies>
		<!-- Java Portlet Specification V2.0 -->
		<dependency>
			<groupId>org.apache.portals</groupId>
			<artifactId>portlet-api_2.0_spec</artifactId>
			<version>1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.7.0</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
			<version>1.7</version>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
			<version>1.7</version>
		</dependency>
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity</artifactId>
			<version>1.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity-tools</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.apache.pluto</groupId>
			<artifactId>pluto-taglib</artifactId>
			<version>1.1.7</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.32</version>
		</dependency>
		<dependency>
			<groupId>org.apache.portals.bridges</groupId>
			<artifactId>portals-bridges-perl</artifactId>
			<version>1.0.4</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.portals.jetspeed-2:</groupId>
					<artifactId>jetspeed-rewriter</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.portals.jetspeed-2</groupId>
			<artifactId>jetspeed-rewriter</artifactId>
			<version>2.1.4</version>
			<exclusions>
				<exclusion>
					<groupId>castor</groupId>
					<artifactId>castor</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>castor</groupId>
			<artifactId>castor</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.portals.bridges</groupId>
			<artifactId>portals-bridges-common</artifactId>
			<version>2.0</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- bind 'pluto2:assemble' goal to 'process-resources' lifecycle -->
			<!-- This plugin will read your portlet.xml and web.xml and injects required
				lines -->
			<plugin>
				<groupId>org.apache.portals.pluto</groupId>
				<artifactId>maven-pluto-plugin</artifactId>
				<version>2.1.0-M3</version>
				<executions>
					<execution>
						<phase>generate-resources</phase>
						<goals>
							<goal>assemble</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- configure maven-war-plugin to use updated web.xml -->
			<!-- This plugin will make sure your WAR will contain the updated web.xml -->
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<webXml>${project.build.directory}/pluto-resources/web.xml</webXml>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<id>copy</id>
						<phase>integration-test</phase>
						<configuration>
							<tasks>
								<copy file="target/${project.artifactId}.war" tofile="${deployFolder}/${project.artifactId}.war" />
							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
					<execution>
						<id>delete</id>
						<phase>clean</phase>
						<configuration>
							<tasks>
								<delete file="${deployFolder}/${project.artifactId}.war" />
								<delete dir="${deployFolder}/${project.artifactId}" />
							</tasks>
							<detail>true</detail>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

As you may notice, the downloaded Apache Perl bridge has already listed inside the Maven file as being it’s now located inside your local repository.

您可能会注意到,下载的Apache Perl桥已经在Maven文件中列出,因为它现在位于本地存储库中。

Perl Portlet和Portlet部署描述符 (Perl Portlet & Portlet Deployment Descriptor)

Just like any other type of Portlets, your Perl Portlet must should look like below:

就像任何其他类型的Portlet一样,您的Perl Portlet必须如下所示:

PerlPortlet.java

PerlPortlet.java

package com.journaldev.portlet;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.portals.bridges.perl.PerlParameters;

public class PerlPortlet extends org.apache.portals.bridges.perl.PerlPortlet{
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException{
		if(request.getParameter("status") != null && request.getParameter("status").equals("success")){
			PerlParameters perlParam =
					(PerlParameters)request.getPortletSession().getAttribute(PerlParameters.PERL_PARAMETER, PortletSession.APPLICATION_SCOPE);
			if(perlParam == null){
				perlParam = new PerlParameters();
				perlParam.setScriptName("success.cgi");
				perlParam.setApplicationName("PerlPortlet");
				request.getPortletSession().setAttribute(PerlParameters.PERL_PARAMETER,perlParam, PortletSession.APPLICATION_SCOPE);
			}
			super.doView(request, response);
		}
		else if(request.getParameter("status") != null && request.getParameter("status").equals("failure")){
			PerlParameters perlParam =
					(PerlParameters)request.getPortletSession().getAttribute(PerlParameters.PERL_PARAMETER, PortletSession.APPLICATION_SCOPE);
			if(perlParam == null){
				perlParam = new PerlParameters();
				perlParam.setScriptName("failure.cgi");
				perlParam.setApplicationName("PerlPortlet");
				request.getPortletSession().setAttribute(PerlParameters.PERL_PARAMETER,perlParam, PortletSession.APPLICATION_SCOPE);
			}
			super.doView(request, response);
		}
		else if(request.getParameter("status") != null && request.getParameter("status").equals("initial")){
			PerlParameters perlParam =
					(PerlParameters)request.getPortletSession().getAttribute(PerlParameters.PERL_PARAMETER, PortletSession.APPLICATION_SCOPE);
			if(perlParam == null){
				perlParam = new PerlParameters();
				perlParam.setScriptName("register.cgi");
				perlParam.setApplicationName("PerlPortlet");
				request.getPortletSession().setAttribute(PerlParameters.PERL_PARAMETER,perlParam, PortletSession.APPLICATION_SCOPE);
			}
			super.doView(request, response);
		}
		else {
			super.doView(request, response);
		}

	}

	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException{
		// Create request dispatcher
		PortletRequestDispatcher dispatcher =  this.getPortletContext().getNamedDispatcher("RegisterEmployeeServlet");
		try {
			if(request.getMethod().equals("POST")){
				// Include
				dispatcher.include(request, response);
				// Set render parameter
				response.setRenderParameter("status", "success");
			}
			else {
				response.setRenderParameter("status", "initial");
			}
		}
		catch(Exception ex){
			// Set render parameter
			response.setRenderParameter("status", "failure");
			response.setRenderParameter("exception", ex.getMessage());
		}

	}
}

portlet.xml

portlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<portlet-app xmlns="https://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
	version="2.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
	<portlet>
		<display-name>PerlPortlet</display-name>
		<portlet-name>PerlPortlet</portlet-name>
		<portlet-class>com.journaldev.portlet.PerlPortlet</portlet-class>
		<description>Per Portlet</description>
		<init-param>
			<name>PerlScript</name>
			<value>register.cgi</value>
		</init-param>
		<init-param>
			<name>ScriptPath</name>
			<value>cgi-bin</value>
		</init-param>
		<init-param>
			<name>Application</name>
			<value>PerlPortlet</value>
		</init-param>
		<supports>
			<mime-type>text/html</mime-type>
			<portlet-mode>VIEW</portlet-mode>
		</supports>
		<portlet-info>
			<title>Perl Portlet</title>
			<keywords>Perl,Portlet</keywords>
			<short-title>Perl Portlet</short-title>
		</portlet-info>
	</portlet>
</portlet-app>

Here’s detailed explanation for the code listed above:

这是上面列出的代码的详细说明:

  • Your Perl Portlet should be of type org.apache.portals.bridges.perl.PerlPortlet.

    您的Perl Portlet的类型应为org.apache.portals.bridges.perl.PerlPortlet
  • You have defined your Perl Portlet either by extending PerPortlet or by mentioning it directly inside your Portlet deployment descriptor and with no need for an existing class that does extend.

    您可以通过扩展PerPortlet或在Portlet部署描述符中直接提及Perl Portlet来定义Perl Portlet,而无需扩展的现有类。
  • doView will be called at each time the Portlet got rendered, as so, it will read the status parameter that determines which Perl view should be delegated for.

    每次渲染Portlet时都会调用doView,因此,它将读取status参数,该参数确定应委派哪个Perl视图。
  • To change the Perl view you should use PerlParameters object that have provided scriptName applicationName.

    要更改Perl视图,应该使用提供了scriptNameapplicationName的 PerlParameters对象。
  • Calling of super.doView() will make sure that the new selected Perl script should be displayed and rendered.

    调用super.doView()将确保应该显示和呈现新选择的Perl脚本。
  • Make sure you’re set the same applicationName just like that name defined in the Portlet.xml.

    确保设置相同的applicationName ,就像在Portlet.xml中定义的名称一样
  • The doView method have defined a default case for which the status parameter might not be not passed or passed with initial value.

    doView方法已定义默认情况,对于该情况,可能不传递状态参数或不传递初始值。
  • Default view will be handled by calling super.doView() with no additional PerlParamaters. Mainly, you should provide PerlScript ScriptPath inside your Portlet.xml for such that purpose.

    默认视图将通过调用super.doView()进行处理,而无需其他PerlParamaters 。 主要是,你应该这样目的提供PerlScript&了ScriptPath里面的portlet.xml。
  • The ProcessAction have defined the all required code for getting an Employee registered.

    ProcessAction已定义了用于注册Employee的所有必需代码。
  • You should note that we’ve distinguished between POST request and GET request in the ProcessAction method due to the Perl Portlet kind. Perl Portlet translates all relative URLs to be Portlet action and so they would call ProcessAction.

    您应该注意,由于Perl Portlet类型,我们在ProcessAction方法中区分了POST请求和GET请求。 Perl Portlet将所有相关URL转换为Portlet操作,因此它们将调用ProcessAction。
  • You can extend this effort by extending the using of render parameters that are set in the ProcessAction to be used in the Perl script.

    您可以通过扩展对在ProcessAction中设置的渲染参数的使用来扩展此工作,以在Perl脚本中使用。

Perl视图 (Perl Views)

We’ve supposed that we have three different Perl Scripts and they’re:

我们假设我们有三个不同的Perl脚本,它们是:

register.cgi

register.cgi

#!D:\Strawberry\perl\bin\perl --
print qq(
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register Employee</title>
</head>
<body>
	<form action="/dummyAction" method="POST">
		<table width="100%">
			<tr width="60%">
				<td>Enter Employee ID:</td>
				<td><input name="employeeID" /></td>
			</tr>
			<tr width="60%">
				<td>Enter Employee Name:</td>
				<td><input name="employeeName" /></td>
			</tr>
			<tr width="60%">
				<td>Enter Employee Job:</td>
				<td><input name="employeeJob" /></td>
			</tr>
			<tr width="60%">
				<td>Enter Employee Salary:</td>
				<td><input name="employeeSalary" /></td>
			</tr>
			<tr width="60%" align="center">
				<td colspan="2"><input type="submit" value="Register" /></td>
			</tr>
		</table>
	</form>
</body>
</html>
);

success.cgi

success.cgi

#!D:\Strawberry\perl\bin\perl --
print qq(

Register Employee
<table width="100%">
<tbody>
<tr>
<td>Registration has done successfully!</td>
<td><a href="/pluto/portal/JournalDev">Register Another</a></td>
</tr>
</tbody>
</table>
);

failure.cgi

failure.cgi

#!D:\Strawberry\perl\bin\perl --

print qq(

Register Employee
<table width="100%">
<tbody>
<tr>
<td>Registration hasn't done successfully!</td>
<td><a href="/pluto/portal/JournalDev">Register Another</a></td>
</tr>
</tbody>
</table>
);

Here’s detailed explanation for the code listed above:

这是上面列出的代码的详细说明:

  • We’ve referenced the perl that’s already installed into your machine. You can install Perl into your machine by downloading the Perl software from here.

    我们引用了已经安装到您的计算机中的perl。 您可以通过从此处下载Perl软件将Perl安装到计算机中。
  • Generally, you’re using #! in Perl to reference your local drives from the Perl script.

    通常,您正在使用#! 在Perl中可以从Perl脚本中引用本地驱动器。
  • We’ve used print function to print out the HTML content.

    我们使用了打印功能来打印出HTML内容。

RegisterEmployeeServlet –业务委托 (RegisterEmployeeServlet – Business Delegation)

We’ve left the business of employee registration into separate Servlet called RegisterEmployeeServlet. It looks like below:

我们已经将员工注册的业务留在了名为RegisterEmployeeServlet的单独Servlet中。 如下图所示:

RegisterEmployeeServlet.java

RegisterEmployeeServlet.java

package com.journaldev.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.journaldev.dao.EmployeeDAO;
import com.journaldev.data.Employee;

public class RegisterEmployeeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(RegisterEmployeeServlet.class);

    public RegisterEmployeeServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Create employee
		Employee employee = new Employee();
		// Fill in required data from the request sent
		employee.setId(Integer.parseInt(request.getParameter("employeeID")));
		employee.setName(request.getParameter("employeeName"));
		employee.setJob(request.getParameter("employeeJob"));
		employee.setSalary(Integer.parseInt(request.getParameter("employeeSalary")));
		try {
			// Asking employeeDAO creating the employee against registered database
			Employee createdEmployee = EmployeeDAO.getInstance().createEmployee(employee);
			// Print out the created employee information
			logger.info("Employee Created"+createdEmployee);
		} catch (Exception e) {
			// Log the exception
			logger.error("Employee Creation Failed", e);
			// Throw another exception for notifying the Portlet
			throw new ServletException(e);
		}
	}
}

数据库实用程序 (Database Utility)

Both of EmployeeDAO & ConnectionUtility classes are used for handling the all database issues.

EmployeeDAOConnectionUtility类都用于处理所有数据库问题。

EmployeeDAO.java

EmployeeDAO.java

package com.journaldev.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.journaldev.dao.utility.ConnectionUtility;
import com.journaldev.data.Employee;

public class EmployeeDAO {

	public static EmployeeDAO employeeDAO = null;

	private EmployeeDAO(){

	}

	public static EmployeeDAO getInstance(){
		synchronized(EmployeeDAO.class){
			if(employeeDAO == null){
				employeeDAO = new EmployeeDAO();
			}

		}
		return employeeDAO;
	}

	public Employee createEmployee(Employee employee) throws SQLException, IllegalAccessException, IOException, ClassNotFoundException{
		// Get connection instance
		Connection connection = ConnectionUtility.getInstance().getConnection();
		// Create Prepared Statement
		PreparedStatement query = connection.prepareStatement("INSERT INTO EMPLOYEE VALUES (?,?,?,?)");
		// Set variables
		query.setInt(1, employee.getId());
		query.setString(2, employee.getName());
		query.setString(3, employee.getJob());
		query.setInt(4, employee.getSalary());

		try {
			// Execute
			query.execute();
			// Return employee instance
			return employee;
		}
		catch(Exception e){
			// Close statement
			query.close();
			// Close connection
			connection.close();
			// Throw another exception for notifying the Servlet
			throw new SQLException(e);
		}
	}

	public boolean deleteEmployee(Employee employee){
		return false;
	}

	public boolean updateEmployee(Employee employee, int employeeId){
		return false;
	}
}

ConnectionUtility.java

ConnectionUtility.java

package com.journaldev.dao.utility;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionUtility {

	private static ConnectionUtility connectionUtiliy = null;

	private Connection connection = null;

	private ConnectionUtility() {
	}

	public static ConnectionUtility getInstance() throws IOException, IllegalAccessException, SQLException, ClassNotFoundException{
		// Synchronized against connectionUtility instance
		synchronized(ConnectionUtility.class){
			// Check whether the connectionUtility is null or not
			if(connectionUtiliy == null){
				// Create a properties instance
				Properties properties = new Properties();
				// Load properties from classpath
				properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("connection.properties"));
				// Set connection with connectionUtility
				connectionUtiliy = new ConnectionUtility();
				// Load driver class
				Class.forName("com.mysql.jdbc.Driver");
				// Create connection
				connectionUtiliy.setConnection(DriverManager.getConnection("jdbc:mysql://localhost:3306/journaldev", properties));
			}
			return connectionUtiliy;
		}
	}

	public Connection getConnection() throws ClassNotFoundException, SQLException, IOException {
		if(connection.isClosed()){
			// Create a properties instance
			Properties properties = new Properties();
			// Load properties from classpath
			properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("connection.properties"));
			// Load driver class
			Class.forName("com.mysql.jdbc.Driver");
			// Create connection
			connectionUtiliy.setConnection(DriverManager.getConnection("jdbc:mysql://localhost:3306/journaldev", properties));
		}
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

}

员工注册演示 (Employee Registration Demo)

Before getting started make a demonstration for how can use the Perl script for employee registration, let’s return you back into an Apache Pluto Introduction that will help you get a ready JournalDev Portal page shown.

在开始演示如何使用Perl脚本进行员工注册之前,让我们回到Apache Pluto 简介中 ,这将帮助您显示就绪的JournalDev Portal页面。

Now, set your Perl Portlet into your JournalDev Portal page and let’s start the process of Employee registration:

现在,将您的Perl Portlet设置到JournalDev Portal页面中,让我们开始员工注册过程:

And if you tried to register the same employee with the same ID you’ll likely get the below response.

而且,如果您尝试使用相同的ID注册同一名员工,则可能会收到以下响应。

摘要 (Summary)

Apache Pluto provides you a huge amount of Bridges that could be used for integrating with different languages. The integration with the Perl language is slightly different, in that, you need to install the Perl software and use the enhanced JournalDev Perl Bridge library to get the Perl scripts displayed into your Portlet.

Apache Pluto为您提供了大量可用于与不同语言集成的桥。 与Perl语言的集成略有不同,因为您需要安装Perl软件并使用增强的JournalDev Perl Bridge库将Perl脚本显示在Portlet中。

Contribute us by commenting below and find the downloaded source code for your experimental purpose.

通过在下面发表评论来为我们贡献力量,并找到用于实验目的的下载源代码。

翻译自: https://www.journaldev.com/5101/apache-pluto-perl-integration-tutorial-example

adalm pluto

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值