javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
lib에 꼭 이 두 jar 파일을 넣고 시작해야 한다.
서블릿을 통해 db로 값을 저장하는 예제
svInput.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="frm" method="post" action="/study_jsp/SvInputServlet">
ID:<input type="text" name="id"><br>
PW:<input type="text" name="pwd"><br>
NM:<input type="text" name="name"><br>
<input type="submit" value="SEND">
</form>
</body>
</html>
====================================================================================
SvInputServlet.java
package servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/SvInputServlet")
public class SvInputServlet extends HttpServlet{
protected void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
String id=req.getParameter("id");
String pwd=req.getParameter("pwd");
String name=req.getParameter("name");
Connection con=null;
Statement stmt=null;
String url="jdbc:mysql://localhost:3306/mysql";
String user="root";
String pw="apmsetup";
String sql="insert into test(id,pwd,name) values('"+id+"','"+pwd+"','"+name+"')";
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(url,user,pw);
stmt=con.createStatement();
int i=stmt.executeUpdate(sql);
stmt.close();
con.close();
if(i>0){System.out.println("success");}
else{System.out.println("error");}
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("ID : "+id);
System.out.println("PWD : "+pwd);
System.out.println("NM : "+name);
}
}
========================================================================
svInput.jsp에서 입력한 값이 form을통해 SvInputServlet.java로 가게 되고
db에 연결과 sql을 통해 db에 저장하게 된다.
'개발 > jsp' 카테고리의 다른 글
jsp (0) | 2015.10.16 |
---|---|
ibatis (0) | 2015.10.15 |
Servlet (0) | 2015.10.13 |
hashmap,foreach,구구단,list (0) | 2015.10.13 |
jstl forTokens,formatNumber (0) | 2015.10.13 |
댓글