简体   繁体   中英

How to hit jsp from java class

How to hit jsp from java class without using servlet? i need parameter from client computer to server( tomcat) then insert into DB.

Below is my java class coding:

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:

<%@ 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM