如何使用jsp页面和tomcat 8编译和运行java文件

时间:2014-03-29 18:23:31

标签: java jsp tomcat servlets

alumini.java:

import java.util.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class  alumini extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException{ 
        String aluno=req.getParameter("aluno");
        String fname=req.getParameter("fname");
        String mname=req.getParameter("mname");
        String lname=req.getParameter("lname");
        String passyr=req.getParameter("passyr");
        String dept=req.getParameter("dept");
        String enroll=req.getParameter("enroll");

        String connectionURL="jdbc:mysql://localhost:3306/test"; //connection variable

        Connection connection = null;
        PreparedStatement st = null;
try { 
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection
    (connectionURL, "root", ""); 
    String sql = "insert into alumini (aluno,fname,mname,lname,passyr,dept,enroll) values (?, ?, ?, ?, ?, ?, ?)";
    st = connection.prepareStatement(sql);
    st.setString(1, aluno);
    st.setString(2, fname);
    st.setString(3, mname);
    st.setString(4, lname);
    st.setString(5, passyr);
    st.setString(6, dept);
    st.setString(7, enroll);
    st.executeUpdate();
    st.close();
    connection.close();
}catch(Exception E){
    System.out.println("The error is=="+E.getMessage());
}
}
}

alumini.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Alumini Form</title>
</head>
<body>
<form action="alumini" method="get" name="alu">
<table>
<tr><td><label>Alumini Number: </label></td><td><input type="text" name="aluno"></td></tr>
<tr><td><label>First Name : </label></td><td><input type="text" name="fname"></td></tr>
<tr><td><label>Middle Name: </label></td><td><input type="text" name="mname"></td></tr>
<tr><td><label>Last Name: </label></td><td><input type="text" name="lname"></td></tr>
<tr><td><label>Year of Passing: </label></td><td><input type="text" name="passyr"></td></tr>
<tr><td><label>Department: </label></td><td><input type="text" name="dept"></td></tr>
<tr><td><label>Enrollment Number: </label></td><td><input type="text" name="enroll"></td></tr>
<tr><td><input type="submit" value="Submit" name="submit"></td></tr>
</table>
</form>
</body>
</html>

的web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <display-name>Servlet in Tomcat</display-name>
  <description>
     This is Servlet
  </description>

<servlet> 
<servlet-name>alumini</servlet-name>
<servlet-class>alumini</servlet-class>
</servlet> 
<servlet-mapping> 
<servlet-name>alumini</servlet-name>
<url-pattern>/alumini</url-pattern>
</servlet-mapping> 
</web-app>

我编译了java文件,并将类文件(alumini.class)放在根文件夹(alumini)中的WEB-INF下的类中。 jsp文件(alumini.jsp)也放在根文件夹中。

问题:

当我使用jsp表单输入时,没有显示任何内容。甚至数据库也没有更新..

1 个答案:

答案 0 :(得分:0)

我弄清楚出了什么问题。 我只是把mysql-connector.jar文件放在了 WEB-INF/lib目录。 最终这解决了我的问题! 现在一切正常。