Using httpservletrequest getlocaladdr

DABE_Help

package de.dbae.servlets; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import javax.servlet.RequestDispatcher; 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 de.dbae.database.DatabaseConnection; import de.dbae.helper.ResultToTable; / ** * Servlet implementation class SQL_suche * / @WebServlet ("/ SQL_suche") public class SQL_suche extends HttpServlet {private static final long serialVersionUID = 1L; / ** * @see HttpServlet # HttpServlet () * / public SQL_suche () {super (); // TODO Auto-generated constructor stub} / ** * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse * response) * / protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException- {// output later Each String musician = request.getParameter ("musician"); String role = request.getParameter ("role"); String mediumArt = request.getParameter ("art"); String songTitel = request.getParameter ("title"); String query = "SELECT name, role, medium_art, song_title" + "FROM medium NATURAL JOIN song NATURAL JOIN musician NATURAL JOIN person" + "WHERE name ILIKE? AND role ILIKE? AND medium_art ILIKE? AND song_title ILIKE?"; Connection connection = DatabaseConnection.getConnection (); try {PreparedStatement statement = connection.prepareStatement (query); statement.setString (1, "%" + musician + "%"); statement.setString (2, "%" + role + "%"); statement.setString (3, "%" + mediumArt + "%"); statement.setString (4, "%" + songTitle + "%"); statement.executeQuery (); // Result as RS ResultSet rs = statement.executeQuery (); ArrayList results = new ArrayList (); // Copy the result to a list while (rs.next ()) {String [] s = {rs.getString (1), rs.getString (2), rs.getString (3), rs.getString (4)} ; results.add (s); } // Forward column names and list request.setAttribute ("columnNames", new String [] {"Musician", "Role", "Medium", "Song"}); request.setAttribute ("resultList", results); RequestDispatcher patcher = request.getRequestDispatcher ("index.jsp"); patcher.forward (request, response); connection.close (); } catch (SQLException e) {e.printStackTrace (); }} / ** * @see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse * response) * / protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Connection con = DatabaseConnection (); // Here I create the variables and query the parameters. String name = request.getParameter ("name"); String role = request.getParameter ("role"); String dataart = request.getParameter ("dataart"); String title = request.getParameter ("title"); try {// Here I set up the SELECT to query the data in the DB PreparedStatement stat = con.prepareStatement ("SELECT name, role, medium_art, song_titel" + "FROM song NATURAL JOIN medium NATURAL JOIN musician" + "NATURAL JOIN person "+" WHERE name LIKE? "+" AND role LIKE? "+" AND medium_art LIKE? "+" AND song_titel LIKE? "); // The% character is a WildCard operator in SQL and is used as a // placeholder stat.setString (1, "%" + name + "%"); stat.setString (2, "%" + role + "%"); stat.setString (3, "%" + dataart + "%"); stat.setString (4, "%" + title + "%"); ResultSet rs = stat.executeQuery (); rs.next (); request.setAttribute ("result", new ResultToTable (). convert (rs)); con.close (); } catch (SQLException e) {request.setAttribute ("result", e.toString ()); } request.getRequestDispatcher ("index.jsp"). forward (request, response); }}