gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:38:08 25 4
gpt4 key购买 nike

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 表单输入时,没有显示任何内容。甚至数据库都没有更新..

最佳答案

我想出了问题所在。我只需要将 mysql-connector.jar 文件放在WEB-INF/lib 目录。最终这解决了我的问题!现在一切正常。

关于java - 如何使用jsp页面和tomcat 8编译运行java文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com