How to pass value from input text to jsp variable?

9.9k views Asked by At

At beginning there is my jsp page code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*" %>
<%@ page import="test.Obiekt" %>
<%@ page import="test.ListaObiektow" %>
<%@ page import="test.Termin" %>
<%@ page import="test.ListaTerminow" %>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>menu główne</title>

    <meta name="viewport" content="initial-scale=1, maximum-scale=1">


    <meta name="android-mobile-web-app-capable" content="yes">
    <meta name="android-mobile-web-app-status-bar-style" content="black">

   <link href="css/ratchet.css" rel="stylesheet">
    <link href="css/ratchet-theme-android.css" rel="stylesheet">


    <script src="js/ratchet.js"></script>

  </head>
  <body>
  <!-- <header class="bar bar-nav">
  <a class="icon icon-left-nav pull-left" href="wyszukaj.html"></a>
  <h1 class="title">Wybierz obiekt</h1>
</header> -->

<div id="content">
   <%
List<Obiekt> list = new ListaObiektow().getObiekty();
%>
<select name="obiekt">
        <option selected value="default">Wybierz Obiekt</option>
        <%
            for (Obiekt obiekt : list) {
        %>
        <option value="<%=obiekt.idObiekt %>"><%=obiekt.nazwa%> <%=obiekt.adres %></option>
        <%
            }
        %>
        <%

        %>
</select> 
<br><br>
<form name="rezerwuj">
    <font color="white">Liczba uczestników</font><input type="text" name="uczest"/>
    <%
        int liczbaUczestnikow = Integer.parseInt(request.getParameter("uczest"));
    %>
</form>    
<br>
<br>
<%List<Termin> lista = new ListaTerminow().getTerminy(); %>
<div id="terminy">
<table>

    <%for (Termin termin : lista) {%>
    <tr>
    <td><%=termin.idTermin %></td>
    <td><%=termin.nazwaObiektu %> <%=termin.adresObiektu %> </td>
    <td><%=termin.dzien %> </td>
    <td><%=termin.odKtorej %> </td>
    <td><%=termin.doKtorej %> </td>
    <td><form action="Zarezerwuj" method="post"><input type="submit" value="Rezerwuj"></form></td>
    </tr>
    <% } %>

</table>
</div>
<br><br><br>

</div>


  </body>
</html>

I want to get value from <input type="text" name="uczest"/> and remember her for submit button which calls mysql query placed in this class:

package test;

import java.io.IOException;

import test.Rezerwacja;

import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test.ConnectionClass;;
/**
 * Servlet implementation class Zarezerwuj
 */
@WebServlet("/Zarezerwuj")
public class Zarezerwuj extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Connection conn; 

    public Zarezerwuj() {
        super();
        conn = ConnectionClass.Polacz();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


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

        int liczbaUczestnikow = Integer.parseInt(request.getParameter("liczbaUczestnikow"));
        int idTermin = Integer.parseInt(request.getParameter("idTermin"));
        Statement st = null;
        String sql = "INSERT INTO rezerwacje (liczbaUczestnikow,idTermin) values ('" + liczbaUczestnikow + "','" + idTermin + "')";
        try
        {
            st = conn.createStatement();
        }
        catch(SQLException e)
        {
            System.out.println(e);
        }
    }

}

What's more i want to pass <td><%=termin.idTermin %></td> termin.idTermin to Zarezerwuj servlet.

I've tried request.getParameter by server calls Jesper Null Exception. I have no idea what to do now.

1

There are 1 answers

2
Neeraj Jain On

This is not how Request#getParameter(nameOfParameter) works :

You first need to create an input field having name attribue as idTermin

Like this :

<input type="text" name="idTermin" value="<%=termin.idTermin %>"/>

Now in your Servlet if you call

request.getParameter("idTermin");

You will get the desired value .

If you don't want an input field you can also create hidden field in your jsp :

<input type="hidden" name="idTermin" value="<%=termin.idTermin %>"/>