67,512
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app version ="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>addEmpServlet</servlet-name>
<servlet-class>web.AddEmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addEmpServlet</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<error-page>
<!--方式1-->
<error-code>500</error-code>
<!--方式2
<exception-type>javax.servlet.ServletException
</exception-type>
-->
<location>/error.html</location>
</error-page>
</web-app>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>addEmp</title>
</head>
<body style="font-size: 30px;">
<form action="add" method="post;">
<fieldset>
<legend>添加雇员</legend>
姓名:<input name="name"/><br/>
薪水:<input salary="salary"/><br/>
年龄:<input age="age"/><br/>
<input type="submit" value="确认";/>
</fieldset>
</form>
</body>
</html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>sorry.</title>
</head>
<body style="font-size: 30px;color: red;">
发生了系统错误,请歇会。
<a href="addEmp.html">try again!~</a>
</body>
</html>
package web;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.PreparedStatement;
import com.sun.net.httpserver.HttpServer;
public class AddEmpServlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//这行代码要放在getparameter()执行之前。
request.setCharacterEncoding("utf-8");
String name =request.getParameter("name");
double salary=Double.parseDouble(request.getParameter("salary"));
int age=Integer.parseInt(request.getParameter("age"));
System.out.println("name:"+name);
System.out.println("salary:"+salary);
System.out.println("age"+age);
//访问数据库
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jd1109db2","root","root");
String sql = null;
java.sql.PreparedStatement prep=conn.prepareStatement(sql);
prep.setString(1, name);
prep.setDouble(2, salary);
prep.setInt(3, age);
prep.executeUpdate();
response.setContentType("text/html;charset=utf-8");
PrintWriter out =response.getWriter();
out.println("添加雇员成功");
out.close();
}catch (Exception e) {
/*异常catch之后,
* 要区分对待不同的异常类型。
*/
/*对于系统异常
* (不是由于程序本身的问题引起的异常,
* 而是由于工作环境,如网络问题,数据库问题等产生的异常
* 如数据库未启动,
* 使用DRIVERMANAGER.GETCONNECTION()是无论如何
* 也得不到连接的,会报异常,这种异常就是系统异常。
* 程序无能为力,但是必须告诉用户。
* 所以还须提示用户
*/
//step1 先记录日志
e.printStackTrace();
//step2 抛出
throw new ServletException(e);
}finally{
if(conn!=null){
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}