gpt4 book ai didi

java - 如何从java类中调用jsp

转载 作者:行者123 更新时间:2023-12-02 12:38:53 25 4
gpt4 key购买 nike

如何在不使用servlet的情况下从java类中调用jsp?我需要从客户端计算机到服务器(tomcat)的参数,然后插入数据库。

下面是我的java类编码:

public void callJSP(String fullContent) {

// convert String into InputStream
InputStream is = new ByteArrayInputStream(fullContent.getBytes());
InputStreamReader isr;
BufferedReader br;
String line;
URL url;
URLConnection connection;
ObjectOutputStream output;
ObjectInputStream input;
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
line = br.readLine();
url = new URL("http://localhost:8080/WebContent/FeedIssueToDB.jsp");
connection = url.openConnection();
connection.setDoOutput(true);
output = new ObjectOutputStream(connection.getOutputStream());
output.writeObject(line);
output.close();
/*input = new ObjectInputStream(connection.getInputStream());
input.readObject();
input.close();*/
// TODO do your stuff here
} catch (Exception ex) {
// TODO
}
}

这是 JSP:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage=""%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
String fullContent= request.getParameter("line");
System.out.println("full content -"+fullContent);

%>
<%

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.55:1433/RTDATA_TT", "sa", "dominorich");
PreparedStatement ps=conn.prepareStatement("INSERT INTO SummaryFeedIssue(\"DateTimeIssue\", \"Source\", \"ServerIP\", \"Keywords\",\"Remarks\") VALUES (?,?,?,?,?)");

StringTokenizer st1 = new StringTokenizer(fullContent, "|");
String dateTime="";
String source="";
String serverIP="";
String keywords="";
String remarks="";
while (st1.hasMoreTokens()) {
dateTime = st1.nextToken();
source = st1.nextToken();
serverIP = st1.nextToken();
keywords = st1.nextToken();
remarks = st1.nextToken();
ps.setString(1,dateTime );
ps.setString(2,source );
ps.setString(3,serverIP );
ps.setString(4,keywords );
ps.setString(5,remarks );

ps.execute();
conn.close();

}

%>

运行应用程序后,它没有任何操作。我的编码有问题吗?

最佳答案

您不应该在 JSP 中编写业务逻辑,而应该有一个具有业务逻辑的管理器或帮助器类,并在 JSP 中调用它们的方法。

但是,

要运行代码,您应该更改行

url = new URL("http://localhost:8080/WebContent/FeedIssueToDB.jsp");

url = new URL("http://localhost:8080/WebContent/FeedIssueToDB.jsp?line="+line);

并且无需将行写入输出流。

将行作为参数传递应该可以完成您想要做的工作。

关于java - 如何从java类中调用jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45031722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com