Code for connecting JSP with MYSQL
JavaServer Pages (JSP) is a Java standard technology that you to
write dynamic, data-driven pages for your Java web applications. JSP is
built on top of the Java Servlet specification.
JSP is the client-side script or markup language and JSP tags are used to to
connect your page to the Java backend ie MYSQL.
The MYSQL database is mainly used for storing various types of data which are huge in size .
JSP can connect with such
databases to create and manage the records.
Here we will type this code in jsp file named data.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="valid.jsp" method="post">
<input type="text" name="name" >
<input type="submit" value="submit">
<input type="password" name="password" >
</form><h1>Hello World!</h1>
</body>
</html>
Here we will type this code in jsp file named valid.jsp
<%@page import="java.sql.Statement"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<%
try
{
String name = request.getParameter("name");
String pass = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysqlr", "root", "");
Statement st = con.createStatement();
st.executeUpdate("INSERT into logintable(name,password) values ('"+name+"','"+pass+"')");
out.println("Valid login credentials");
}
catch(Exception e)
{
out.println("e");
}
%>
</body>
</html>
Good article ma'am, although some common questions like What is JSP and why should anyone consider using it, should also be answered in a follow up blog post. That'll entice the readers to consider reading the articles and trying out the code snippets as well.
ReplyDelete