这篇文章总字数为:2616 字,有 0 张图存于本站服务器
实验总结:
这个作业没什么好说的,老师PPT已经给代码且给步骤了,
唯一值得说的是,你懒得自己打代码,或者你“没时间”!
另一个作业实验7-1,值得一说,老师得PPT代码不全,而且有一处错误。
效果预览:
视频步骤:(2分34秒)
“小白观看”
文章底部可下载完整代码,作业可以只交源码和截图
实验代码:
记得删改学号姓名!!!
register.html文件(位置:根目录/WebRoot/)
<!DOCTYPE html>
<html>
<body>
<form action="servlet/register" method="post">
输入账号:<input type="text" name="user"><br><br>
输入密码;<input type="password" name="pwd"><br><br>
<input type="submit" value="注册">
</form>
</body>
</html>
register.java文件(位置:根目录/src/servlet/)
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class register extends HttpServlet {
/**17180074平帅作业7-1
*
*/
private static final long serialVersionUID = -6390129093592980992L;
public void destroy() {
super.destroy();
System.out.println("destroy方法");
}
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
//从request接收表单数据
request.setCharacterEncoding("GBK");
String name=request.getParameter("user");
String pass=request.getParameter("pwd");
//通过response向客户端应答,显示接收到的数据
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>处理表单</TITLE></HEAD>");
out.println("<BODY>");
out.println(name+"你好,");
out.println("请记住你的密码"+pass+"!");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init() throws ServletException {
System.out.println("init方法");
}
}
web.xml文件(位置:根目录/WebRoot/WEB-INF/)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>register</servlet-name>
<servlet-class>servlet.register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/servlet/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
评论