[原创]基于weblogic实现JSP、servlet、JDBC_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2279 | 回复: 0   主题: [原创]基于weblogic实现JSP、servlet、JDBC        下一篇 
tianyuan.liu
注册用户
等级:上尉
经验:766
发帖:33
精华:0
注册:1970-1-1
状态:离线
发送短消息息给tianyuan.liu 加好友    发送短消息息给tianyuan.liu 发消息
发表于: IP:您无权察看 2015-1-13 15:03:11 | [全部帖] [楼主帖] 楼主

下载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中配置。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论