0

I have a JSP page with a textarea and a button, a servlet and a new jsp:

My 3 problems are:

  1. My analysis.jsp is empty. My borwser opens a new tab but without the text "Hello World"
  2. The variable "sql" in my java class is empty too. It should contain the text from my textarea
  3. How can I hand over after my variable sql isn't empty (after analyze) new new code to a new jsp

index.jsp

<form action="ServletName" method="post" target="_blank">
    <div class="form-group">
       <label for="codeEditor" style="margin-top: 15px;">Code:</label>
            <textarea name="codeEditor" class="form-control" id="codeEditor" rows="15" style="resize: none;"></textarea>
    </div>

    <div class="analysisButton">
        <button type="submit" id="startAnalysis" class="btn btn-default btn-block" style="margin-top: 10px;">Start analysis</button>        
    </div>
</form>

ServletName.java

protected void doPost(...) ...{
    String sql = request.getParameter("codeEditor");

    response.sendRedirect("/analysis.jsp");
}

analysis.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>

Many thanks in advance

UPDATE: nevermind the variable sql isn't empty <(^,^<) but the other 2 questions are open :)

xNappy
  • 158
  • 1
  • 4
  • 12
  • 1
    Forward your query to servlet and inside this call your java class's method. – SatyaTNV Feb 04 '16 at 14:24
  • can you tell me a little bit more? :/ I am not an pro in query/jsp – xNappy Feb 04 '16 at 14:27
  • 1
    This question is **very** broad. For one the textarea isn't "in the JSP" in any meaningful sense - it is in the browser on the client side. How you get than data back to the server side is an extremely broad topic starting with TCP/IP and ending with Ajax and WebSockets. See [here](https://en.wikipedia.org/wiki/Client%E2%80%93server_model). – Boris the Spider Feb 04 '16 at 14:31
  • Why is my question a dublicate? May the resolved problem (jsp to servlet) is resolved but there are 2 other open questions. The refer ist just about jsp to servlet – xNappy Feb 08 '16 at 10:10
  • 1
    Just ask one Question per question. That's easier finding answers, getting answers and accepting one Answer. The duplicate is based on your currently accepted Answer. All those beginner's questions have been chewed out so many times across years here, so don't surprise when one gets closed as duplicate because another beginner coincidentally asked the same question before. – BalusC Feb 08 '16 at 13:05

2 Answers2

1

Yes, this may be as simple as

JSP

<form action="some_url" method="post" >
<inputarea name="sqlQuery"  >
<input type="submit" value="Sql query" >
<form >

In your servlet, you'll have something like

Servlet

 public void doPost(HttpServletRequest request, HttpServletResponse 
        response) throws IOException, ServletException  {

 ...//check that the request is correct
 String query = request.getParameter("sqlQuery");//name of textarea
 try{
 Connection conn = getConnection();//Search SO for how to get a connection 
 PreparedStatement stmt = conn.prepareStatement(query);
//if your query has any arguments(?) (e.g select * from tbl where id=?),  then you should set them too
 //stmt.setInt(1, 1000);
ResultSet rs = stmt.executeQuery();
while(rs.next()){

//get database dana here
int someIntVal = rs.getInt("intColumn");
String someStr = rs.getString("someStringColumn");
}catch(SQLException e){
//handle exception
}
}
dsp_user
  • 2,061
  • 2
  • 16
  • 23
0

You could do this using a simple form submit:

<form method="POST" action="/myServletPath">
   <inputarea name="sqltext" ...

Your servlet (with url mapping 'myServletPath') can then, in the doPost method:

String sql = request.getParameter("sqltext")...

And then do whatever you want.

WARNING: If this is code for your production application, then be aware of SQL injection attacks. This is typically code you wouldn't write for any application that trusts users other than yourself.

ernest_k
  • 44,416
  • 5
  • 53
  • 99