jsp, I want to use servlet and add the contents of the form to the database

Asked 2 years ago, Updated 2 years ago, 143 views

From test_db.jsp to test_db.java display works well, but the results do not reflect after that.I am using Eclipse ver.2019-03 (4.11.0).

I look forward to hearing from you.

test_db.jsp

<%@pagelanguage="java" contentType="text/html;charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>User Registration</title>
</head>
<body>
<form action="test_db" method="post">
Name: <br>
<input type="text" name="name"><br>
Gender: <br>
Man <input type="radio" name="gender" value="0">
Woman <input type="radio" name="gender" value="1">
<input type="submit" value="Register">
</form>
</body>
</html>

test_db.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * servlet implementation class FormSampleServlet
 */
@ WebServlet ("/test_db")
public class test_db extensions HttpServlet{
    private static final long serialVersionUID = 1L;

    // instantiated value passing
    // ConnectionClassobj = new ConnectionClass();

    /**
     * @see HttpServlet#doPost (HttpServletRequest request, HttpServletResponse response)
     */

    static final String URL="jdbc:mysql://localhost/data_data";
    static final String USERNAME="raraio";
    static final String PASSWORD="Vwxyz123";

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

        // Get request parameters
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");

        // Check request parameters
        String errorMsg="";
        if(name==null||name.length()==0){
            errorMsg+="No name entered<br>";
        }
        if(gender==null||gender.length()==0){
            errorMsg+="Gender not selected <br>";
        } else{
            if(gender.equals("0")}
                gender="male";
            } else if(gender.equals("1")}
                gender="female";
            }
        }

        // Message to display
        String msg = ' ' + name + ' ' , ' + gender + ' ;
        if(errorMsg.length()!=0){
            msg = errorMsg;
        }

        // Output HTML
        response.setContentType("text/html; charset=UTF-8";
        PrintWriter out=response.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<metacharset=\"UTF-8\">");
        out.println("<title>User Registration Results</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<p>"+msg+"</p>");
        out.println("</body>");
        out.println("</html>");

    }

}

test_db_connection.java

package com.developersio;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class test_db_connection{

    static final String URL="jdbc:mysql://localhost/data_data";
    static final String USERNAME="raraio";
    static final String PASSWORD="Vwxyz123";

    public static void main(String[]args) {

        try(
                Connection connection = DriverManager.getConnection (URL, USERNAME, PASSWORD);
                Statement statement = connection.createStatement();
                ) {

            String sql = "INSERT INTO test VALUES(msg);";";
//          String sql = "INSERT INTO test VALUES('matuda','man');";
            int result = statement.executeUpdate(sql);
            System.out.println("Result 1:" + result);

//            sql = "INSERT INTO music (name, title, year) VALUES('Aril');";
            sql = "INSERT INTO test VALUES('tanaka', 'woman');";
            result=statement.executeUpdate(sql);
            System.out.println("Result 2:" + result);

        } catch(SQLExceptione){

            e.printStackTrace();

        }

    }

}

java database jsp servlet

2022-09-30 10:54

1 Answers

You can enter a value in the form above jsp and submit it to the html page that you are outputting in the servlet.However, the result is not added to the DB.

The process of registering with DB is implemented in class test_db_connection, but no records are added because it is not called from anywhere.Try moving the DB access code for test_db_connection.java to test_db.java.There are many other things that need to be corrected and improved, but I guess that's where we start.


2022-09-30 10:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.