How to run servlets in eclipse

心已入冬 提交于 2019-12-23 01:37:28

问题


i am new to eclipse environment. I downloaded eclipse helios and tomcat 6. I configured them properly. Now my job is to create servlet for some sign in form. I have been given some existing servlet file from my company. I just need to modify it. Could anybody tell me how to run my existing servlet file? How to connect the file with my mysql table?


回答1:


Steps to create java web application:
1.Create new Dynamic Web Project
2.Copy existing servlet file to src folder
3.Create web.xml file in WebContent/WEB-INF folder
4.Configure web.xml, for example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
        xmlns="http://java.sun.ru/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.ru/xml/ns/javaee http://java.sun.ru/xml/ns/javaee/web-app_2_5.xsd" 
        version="2.5">
 <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>test.HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

replace test.HelloServlet by your servlet class, servlet name also may be replaced
5.Open Server view in Eclipse, add new Tomcat Server using context menu, publish the project and run server.




回答2:


From your question, I'm guessing you need a way of connecting a servlet to a MySQL database. If that is the case, then below are the steps:

  1. Use the MySQL jdbc driver for making the connection. You can download the jdbc driver for MySQL from here and then put the driver jar file into the classpath.

  2. You need to create the table in MySQL database and then connect it through JDBC to show all the records present there. Below is the structure:

    CREATE TABLE `servlet` (
    `id` int(11) NOT NULL auto_increment,
    `name` varchar(256) default NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    /*Data for the table `servlet` */
    insert into `servlet`(`id`,`name`) values (1,'sandeep'),(2,'amit'),(3,'anusmita'), (4,'vineet');
  3. Create the servlet and connect to the database:

// *DataBase Connectivity from the Servlet.
import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection extends HttpServlet {

  public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("");
  out.println("Servlet JDBC");
  out.println("");
  out.println(" Servlet JDBC");
  out.println("");  

  // connecting to database
  Connection con = null;  
  Statement stmt = null;
  ResultSet rs = null;

  try {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql://192.168.10.59:3306/example","root","root");
      stmt = con.createStatement();
      rs = stmt.executeQuery("SELECT * FROM servlet");

      // displaying records
      while(rs.next()){
          out.print(rs.getObject(1).toString());
          out.print("\t\t\t");
          out.print(rs.getObject(2).toString());
          out.print("
"); } } catch (SQLException e) { throw new ServletException("Servlet Could not display records.", e); } catch (ClassNotFoundException e) { throw new ServletException("JDBC Driver not found.", e); } finally { try { if(rs != null) { rs.close(); rs = null; } if(stmt != null) { stmt.close(); stmt = null; } if(con != null) { con.close(); con = null; } } catch (SQLException e) {} } out.close(); } }

I think you get the idea by now.



来源:https://stackoverflow.com/questions/9113497/how-to-run-servlets-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!