2020-09-24
MyScipse基础**
ReqServlet.java
package org.lzl;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lzl.util.DBUtil;
public class RegServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//首先全部编码改为utf-8
//将请求中的编码设置为utf-8
request.setCharacterEncoding("utf-8");
//将响应中的编码设置为utf-8
response.setCharacterEncoding("utf-8");
//设置响应的显示格式(类型)
response.setContentType("text/json");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String userName = request.getParameter("userName");
String ageStr = request.getParameter("ageStr");
//将ageStri类型转换
Integer age = Integer.parseInt(ageStr);
boolean isTure = false;
//未来添加的代码区域
Connection conn = DBUtil.getConn();
String sql = "insert into users(name,age,pwd,user_name) values(?,?,?,?)";
PreparedStatement ps = null;
//注册业务--》新增-->
try {
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, pwd);
ps.setString(4, userName);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
int count = ps.executeUpdate();
if(count>0){
isTure = true;
}else{
isTure = false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.close(conn, ps, null);
//处理响应
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(isTure){
out.println("<h1 style = 'color:green'>注册成功!</h1>");
}else{
out.println("<h1 style = 'color:red'>注册失败!</h1>");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
DBUtil
package org.lzl.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static String driver = “”
+ “com.microsoft.sqlserver.jdbc.SQLServerDriver”;
private static String url = ""
+ "jdbc:sqlserver://localhost:1433;databaseName=school";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "sa", "1");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
index,jsp
<%@ page language=“java” import=“java.util.*” pageEncoding=“utf-8”%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"????/"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
用户名:
密码:
确认密码: <input type ="password" name="pwd"/>
姓名:
年龄:
</body>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
index.jsp
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>org.lzl.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>regServlet</servlet-name>
<servlet-class>org.lzl.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>regServlet</servlet-name>
<url-pattern>/regServlet</url-pattern>
</servlet-mapping>
上一篇: 建立您的启动:错误记录
下一篇: 哈工大2019年春算法设计与分析期末复习
推荐阅读