简体   繁体   中英

How to call java servlet in HTML Select Form

<!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>


        <select name="obiekty" id="obiekty">

        </select>




<button class="btn btn-block">Dalej<span class="icon icon-right"></span></button>

  </body>
</html>

Hello. This is my html code. I want to put into...

<select name="obiekty" id="obiekty">

</select>

... this section, where i could choose item (WHICH IS SELECTED FROM DATABASE).

Here is my servlet code:

package servlety;

import java.sql.*;

import javax.servlet.annotation.WebServlet;

@WebServlet("obiekty")
public class WyswietlObiekty {




    public static void showObjects(String dyscyplina)
    {
        Connection conn = null;  
        PreparedStatement pst = null;  
        ResultSet rs = null;  

        String url = "jdbc:mysql://localhost:3306/";  
        String dbName = "zespolowy";  
        String driver = "com.mysql.jdbc.Driver";  
        String userName = "adminek";  
        String password = "123";
        try {  
            Class.forName(driver).newInstance();  
            conn = DriverManager  
                    .getConnection(url + dbName, userName, password);  

            pst = conn.prepareStatement("select nazwa,adres from obiekty where dyscyplina='pilkanozna';");

            rs = pst.executeQuery();  


        } catch (Exception e) {  
            System.out.println(e);  
        } finally {  
            if (conn != null) {  
                try {  
                    conn.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }  
            }  
            if (pst != null) {  
                try {  
                    pst.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }  
            }  
            if (rs != null) {  
                try {  
                    rs.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }  
            }  
        }
    }

}

The question is: How to call this servlet, that i could see all items from database.table in SELECT SECTION ?

If you want to call the servlet from the HTML page the below are the possible ways.

1.Form Submission 2.Links 3.on Event generation by using javascript and ajax we will call

In all the above cases we will give the url pattern of the servlet and the parameters.

Example :

Form :

Here /loginAction is the url pattern of the servlet component.

Link :

ClickHere

In the same way on generation of Events on the html page we will call javascript function and We will submit the request to servlet using ajax calls

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