现在的位置: 首页 > 综合 > 正文

4、JSP与Servlet交互,JSP与JSP交互

2018年02月06日 ⁄ 综合 ⁄ 共 4438字 ⁄ 字号 评论关闭

1、JSP与Servlet交互,在jsp中form的action为servlet:register.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="RegisterServlet" method="post">
    	username:<input type="text" name="username"><br>
    	password:<input type="password" name="password"><br>
    	repassword:<input type="password" name="repassword" ><br>
    	age:<input type="text" name="age"><br>
    	<input type= "submit" value="submit">
    	<input type="reset" value="reset">
    </form>
  </body>
</html>

RegisterServlet:

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
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String repassword = req.getParameter("repassword");
		int age = Integer.parseInt(req.getParameter("age"));
		String result = "";
		if(password.equals(repassword) && age > 18)
		{
			result += "success:";
		}
		if(!password.equals(repassword))
		{
			result +="pass!=repass";
		}
		if(age <= 18)
		{
			result +="age <= 18";
		}
		
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		
		out.println("<html><head><title>register result</title></head>");
		out.println("<body><h1>" + result + "</h1></body></html>");
		
		out.flush();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		this.doGet(req, resp);
	}
}

register.jsp中的form的action值为一个servlet,将其改为一个jsp:result.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("username");
     	String password = request.getParameter("password");
     	
     	out.println("username:" + username +"<br>");
     	out.println("password:" + password + "<br>");
     %>
  </body>
</html>

jsp也可以处理,注意jsp中的<%   %>中的内容是java程序,用于处理请求。

也可以直接在浏览器的地址栏输入servlet的资源地址,来直接调用servlet,这里地址栏输入的是web.xml中url-pattern值:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DoubleColorBallServlet extends HttpServlet
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		Set<Integer> set = new HashSet<Integer>();
		
		Random random = new Random();
		
		while(set.size() != 6)
		{
			int randomValue = random.nextInt(33) +1;
			
			set.add(randomValue);
		}
		
		int randomValue = random.nextInt(16) + 1;
		
		String result = "red ball:";
		
		for(Integer i : set)
		{
			result += i + " ";
		}
		result += "<br>blue ball:" + randomValue;
		
		resp.setContentType("text/html");
		
		PrintWriter out = resp.getWriter();
		
		out.println("<html><head><title>result</title></head>");
		out.println("<body>" + result + "</body></html>");
		
		out.flush();
	}
}

上面是一个servlet,web.xml中的配置:

<servlet>
    <servlet-name>DoubleColorBallServlet</servlet-name>
    <servlet-class>com.cdtax.servlet.DoubleColorBallServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>DoubleColorBallServlet</servlet-name>
    <url-pattern>/DoubleColorBallServlet</url-pattern>
</servlet-mapping>

可以在地址栏直接输入:http://localhost:8080/webp1/DoubleColorBallServlet进行访问。因为通过浏览器地址栏进行的访问全部都是GET方法的,所以servlet只需要实现doGet方法。

抱歉!评论已关闭.