概述
写一个注册,注册页面用jsp写jsp里面有username,password,惹psssword,age。提交之后转到servlet。servlet:判断password==repassword和age>18,如果都符合,显示success。不成功:1,!password==repassword。2age<18或者两个都显示
1,jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.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">
-->
</head>
<body>
<form action="result.jsp">
用户名=<input type="text" name="username1"><br>
密码=<input type="password" name="password1"><br>
确认密码=<input type="password" name="repassword1"><br>
年龄=<input type="text" name="age1"><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
2,servlet代码如下
package firstservlet;
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 RegisterServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String username=request.getParameter("username");
String password=request.getParameter("password");
String repassword=request.getParameter("repassword");
int age=Integer.parseInt(request.getParameter("age"));
String result="";
if(password.equals(repassword)&&age>18)
{
result+="success";
}
if(!password.equals(repassword))
{
result+="password!=repassword";
}
if(age<=18)
{
result+="age<18";
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("<h1> "+result+"<h1>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
在编写servlet时,可以直接创建一个servlet。相关的doget和dopost方法,以及xml配置都会自动生成,其效果和手写是一样的。
我们将注册的结果提交给jsp,用jsp获取用户输入的信息,并将其显示出来。新建一个result.jsp
只需在register.jsp中form的action设置为result.jsp
该jsp的代码为:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.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">
-->
</head>
<body>
<% String username=request.getParameter("username1");
String password=request.getParameter("password1");
String repassword=request.getParameter("repassword1");
int age=Integer.parseInt(request.getParameter("age1"));
out.println("username:"+username+"<br>");
out.println("password:"+password+"<br>");
out.println("repassword"+repassword+"<br>");
out.println("age:"+age+"<br>");
%>
</body>
</html>
最后
以上就是任性便当为你收集整理的jsp+servlet小练习的全部内容,希望文章能够帮你解决jsp+servlet小练习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复