#Connecting jsp to java class in Servlet
10 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @versed harbor! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
bmi.java (Java class)
` package bmi1;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@WebServlet("/bmi")
public class bmi extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("id");
String password = request.getParameter("password");
ServletContext sc = (ServletContext)getServletContext();
PrintWriter pw = response.getWriter();
response.setContentType("text/html; charset=Shift_JIS");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/csd_5", "root", "#1Hateno");
System.out.println("yes 1");
Statement st = con.createStatement();
String query1 = "select * from login where phone_number = "+username+" and password="+password;
ResultSet rs = st.executeQuery(query1);
System.out.println("yes 2");`
String query2 ="select student.first_name from login, student where login.phone_number='"+username+"'and login.phone_number=student.phone_number";
ResultSet rs2 = st.executeQuery(query2);
if(rs2.next()) {
pw.println("<font color=black size=18>Welcome "+"");
String query3 = "select date, weight, height, bmi from bmi, login, student where "+ "login.phone_number= "+username+" and login.phone_number=student.phone_number and student.rd=bmi.rd";
ResultSet rs3 = st.executeQuery(query3);
while(rs3.next()) {
pw.println("<html>\n"
+"<table>"
+"<tr>"
+"<th>Date</th>"
+"<th>Weight</th>"
+"<th>Height</th>"
+"<th>Bmi</th>"
+"</tr>"
+"<tr>"
+"<td>"+rs3.getString(1)+"</td>"
+"<td>"+rs3.getString(2)+"</td>"
+"<td>"+rs3.getString(3)+"</td>"
+"<td>"+rs3.getString(4)+"</td>"
+"</tr>"
+"</table>"
+ "</html>\n");
}
}
}
else {
pw.println("<font color=red size=18>Login failed!!!");
}
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace() ;
} catch (SQLException e) {
e.printStackTrace();
System.err.println(e.getMessage());
};
}
} ```
This message has been formatted automatically. You can disable this using /preferences.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0"> <display-name>bmi1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>login.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>default.htm</welcome-file> </welcome-file-list> </web-app>
What does actually happen?
here first try to receive the values from the parameters and put a sout /print out
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.