[英]How to hit jsp from java class
How to hit jsp from java class without using servlet? 如何在不使用servlet的情况下从Java类中打JSP? i need parameter from client computer to server( tomcat) then insert into DB. 我需要从客户端计算机到服务器(tomcat)的参数,然后将其插入数据库。
Below is my java class coding: 以下是我的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
}
}
this is JSP: 这是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();
}
%>
After run the application, it no action.Any wrong for my coding? 运行该应用程序后,它没有任何作用。我的编码有错吗?
You should not write you business logic in the JSPs, rather you should have a manager or a helper class having the business logic and call their methods in the JSPs. 您不应该在JSP中编写业务逻辑,而应该拥有一个具有业务逻辑的管理器或助手类,并在JSP中调用它们的方法。
But, 但,
To run your code, you should change the line 要运行代码,您应该更改行
url = new URL("http://localhost:8080/WebContent/FeedIssueToDB.jsp");
to 至
url = new URL("http://localhost:8080/WebContent/FeedIssueToDB.jsp?line="+line);
and no need to write the line to output stream. 无需将行写入输出流。
Passing the line as parameter should do the work which you are intended to do. 将行作为参数传递应该完成您打算要做的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.