⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsphandler.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission. 

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/JspHandler.cpp,v $
 * $Date: 2003/05/13 18:42:18 $
 *
 Description:
	Execute an servlet extension.

\*____________________________________________________________________________*/
//$SourceTop:$

#include <iostream.h>
#include <stdio.h>
#include <stdarg.h>

#include "JspHandler.h"
#include "FlexLexer.h"
#include "jsp_parser.h"
#include "jsp_scanner.h"

#ifdef POSIX
#	include <unistd.h>
#	include <errno.h>
#	include <sys/wait.h>
#endif

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	Jsp

Description:
	Parse, compile and execute a JSP page. You can find more informations
	about JSP and Servlets here <A HREF="http://java.sun.com/">
    http://java.sun.com/</A>.

Options:

<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>JavaCompiler
<TD>"javac"
<TD>Quoted string
<TD>Optional command line for the java compiler
<TD>"/usr/local/jdk1.3.1/bin/javac"

<TR>
<TD>ClassPath
<TD>-
<TD>Quoted string
<TD>Optional classpath set for the java compiler
<TD>"Pi3Servlet.jar;servlet.jar"

<TR>
<TD>Context
<TD>+
<TD>Valid Pi3 object Reference
<TD>Reference to servlet container
<TD>ServletContainer

<TR>
<TD>Extends
<TD>-
<TD>Quoted string
<TD>Valid base class used by this Jsp.
<TD>"Pi3HttpJspPage"

<TR>
<TD>Init
<TD>-
<TD>Quoted string
<TD>Servlet config option(s)
<TD>"color=red"

<TR>
<TD>Encoding
<TD>-
<TD>Quoted string
<TD>Encoding of the java source files
<TD>"UTF8"

<TR>
<TD>OutputPath
<TD>-
<TD>Quoted string
<TD>Valid path name, where the compiled Jsp source files (.class) will be stored
<TD>"c:\Program Files\jdk1.3.1\jre\lib"

<TR>
<TD>SourcePath
<TD>-
<TD>Quoted string
<TD>Valid path name, where the Jsp source files (.java) will be stored
<TD>"..\Jsp"

</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>

<H4>Description of Options</H4>
<H5>Context, Init</H5>
This options are the same as used for the class Servlet.

<H5>JavaCompiler</H5>
The java compiler will be invoked on POSIX systems by this command line.
On Windows, the <i>sun.tools.javac.Main</i> interface is used.

<H5>ClassPath</H5>
The java compiler will use this as the class path, if set, when a Jsp source is compiled.

<H5>Extends</H5>
This parameter is a valid class name, to be used as the base class of a Jsp
by the Jsp parser. This value is optional and needs to be overwritten only
in special cases.

<H5>Encoding</H5>
This optional parameter sets the encoding of the .java files for the javac compiler.
The default value is UTF8.

<H5>OutputPath</H5>
The java compiler needs a valid path name, where the compiled Jsp source files
(.class) will be stored. This path must be part of the CLASSPATH.

<H5>SourcePath</H5>
The built in JSP parser will write the output Jsp source files (.java) into this
folder. The java compiler looks for this source files in this folder.

Phase:
	HANDLE

Returns:
	PIAPI_COMPLETED, PIAPI_ERROR or INT_REDIRECT according to the result of
	JNI calls and the status returned by the servlet.

Note:
Example:
	<PRE>
	&lt;Object&gt;
		Name Jsp
		Class JspClass
		Context ServletContainer
	&lt;/Object&gt;

	&lt;Object&gt;
		...
		Handle Condition="&cmp(&dblookup(response,string,ObjectMap,SERVLET),Servlet)" Jsp
		...
	&lt;/Object&gt;
	</PRE>

/*___+++HTMLDOC_END+++___*/
#endif


int Jsp::Parameter( const char *pVariable, const char *pValue,
	const char *pWhere )
	{
	assert( pVariable && pValue );
	PIOStrStream os;
	os << pWhere << GetLogName() << ": ";

	if ( !PIUtil_stricmp( KEY_CONF_EXTENDS, pVariable ) )
		{
		sExtendsName = (const char *)DeQuote( pValue );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_SOURCEPATH, pVariable ) )
		{
		sSourcePath = (const char *)DeQuote( pValue );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_OUTPUTPATH, pVariable ) )
		{
		sOutputPath = (const char *)DeQuote( pValue );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_ENCODING, pVariable ) )
		{
		sEncoding = (const char *)DeQuote( pValue );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_CLASSPATH, pVariable ) )
		{
		sClassPath = (const char *)DeQuote( pValue );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_COMPILER, pVariable ) )
		{
		sCompiler = (const char *)DeQuote( pValue );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_CLASSNAME, pVariable ) )
		{
		}
	else if ( !PIUtil_stricmp( KEY_CONF_SERVLETNAME, pVariable ) )
		{
		}
	else if ( !PIUtil_stricmp( KEY_CONF_INITPARAM, pVariable ) )
		{
		}
	else if ( !PIUtil_stricmp( KEY_CONF_SERVLETCONTEXT, pVariable ) )
		{
		}
	else 
		{
		return Servlet::Parameter( pVariable, pValue, pWhere );
		};

	return 1;
	};

Jsp::Jsp( PIObject *pObject, int iArgc, const char *ppArgv[], const char *pLogName )
	:	Servlet( pObject, iArgc, ppArgv, pLogName ),
		sExtendsName( DEFAULTEXTENDS ),
		sClassPath( 0 ),
		sSourcePath( 0 ),
		sOutputPath( 0 ),
		sEncoding( "UTF8" ),
		sCompiler( "javac" )
{
	if (IsOK()) { ReadParameters( iArgc, ppArgv ); };
};


int Jsp::Compile( PIHTTP &tPIHTTP, const char *pSourcePath )
{
	int iRet = PIAPI_COMPLETED;
#ifdef WIN32
    JNIEnv *env = 0;       /* pointer to native method interface */

	jclass ServletUtilsClass = 0;
	jobject ServletUtilsObject = 0;
	jclass JspCompilerClass = 0;
	jobject JspCompilerObject = 0;
	jthrowable Exception = 0;

	PIDB *pR = tPIHTTP.pResponseDB;

	if (!PIHTTP_isChild( &tPIHTTP )) {
		iRet = JVM()->AttachCurrentThread((void **)&env, NULL);
		if (iRet) {
			Log( &tPIHTTP, "%s: Error when trying to instantiate JVM.", GetLogName());
			return PIAPI_ERROR;
		} else {
			Log( &tPIHTTP, "%s: JVM attached.", GetLogName());
		};
	} else {
		env = (JNIEnv *)PIDB_lookup( pR, PIDBTYPE_OPAQUE, "SERVLET_ENV", 0 );
	};
	
	/* get the ServletUtils class */
	ServletUtilsClass = env->FindClass("org/pi3/servlet/util/Pi3ServletUtils");
	if (!ServletUtilsClass) { goto init_error; }

	/* get the ServletUtils constructor */
	jmethodID ServletUtilsMID;
	ServletUtilsMID = env->GetMethodID(ServletUtilsClass, "<init>", "(II)V");
	if (!ServletUtilsMID) { goto init_error; };

	/* get the ServletUtils object */
	ServletUtilsObject = env->NewObject( ServletUtilsClass, ServletUtilsMID,
		(int)&tPIHTTP, (int)this);
	if (!ServletUtilsObject) { goto init_error; };

	/* get the Pi3ServletUtils.grabStackTrace method */
	jmethodID grabStackTraceMID;
	grabStackTraceMID = env->GetMethodID(ServletUtilsClass, "grabStackTrace",
		"(Ljava/lang/Throwable;)Ljava/lang/String;");
	if (!grabStackTraceMID) { goto init_error; };

	/* get the JspCompiler class */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -