下载WebLogic for Eclipse插件,将WebLogic集成到Eclipse。在Eclipse中新建dynamic web project。
在webcontent目录下新建index.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>
Hello
<a href="http://localhost:7001/test3/TestServlet">testServlet</a>
</body>
</html>
在Java源文件目录下新建测试servlet文件,内容如下:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
//@WebServlet(name="TestServlet",urlPatterns="/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("222222222");
try{
Connection con = null; // 定义一个MYSQL链接对象
Class.forName("com.mysql.jdbc.Driver").newInstance(); // MYSQL驱动
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/tianyuan",
"root", "mysql"); // 链接本地MYSQL
Statement stmt; //创建声明
stmt = con.createStatement();
//新增一条数据
stmt.executeUpdate("INSERT INTO test (id, name) VALUES (2,'efg')");
ResultSet res = stmt.executeQuery("select LAST_INSERT_ID()");
int ret_id;
if (res.next()) {
ret_id = res.getInt(1);
System.out.print(ret_id);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
response.sendRedirect("test.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
使用注解配置servlet,所以不需要再web.xml中配置。