• [JavaWeb基础] 002.JSP和SERVLET初级入门


      上一篇中,我介绍了javaweb项目的创建和Tomcat的搭建和部署,接下来我们要在上一篇的基础上去讲解一下简单的jsp和servlet交互,做出一个简单的登陆功能页面。该例子主要讲解从页面请求道后台处理再分发到前台的一个简单流程

    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 '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">
    	-->
      </head>
      <style type="text/css">
    	body{text-align:center;}
      </style>
      <body>
        <form action = "Login" method = "post">
            username:<input type="text" name = "username"/><br/><br/>
            userid:<input type="text" name = "userid"/><br/><br/>
            <input type="submit" value="提交"/>
        </form>
      </body>
    </html>
    

     这个页面我们主要关注body里边的内容,也就是form标签以内的。form是个表单,配置的action是要对应WEB-INF下面web.xml里头配置的servlet名称,后面介绍,method有2中取值,一种是Get,一种就是post为了数据安全,一般用post来提交表单。表单里面有 username和userid 2个输入框,name后面用来取值,注意输入。然后还有一个提交按钮,直接type=submit就能自动提交了。

    2.有了前端页面了,那么前端提交请求要跳转到对应的配置的action,那么我们就要在WEB-INF下面的web.xml中进行配置

    这样我们就配置好了servlet,接下来我们就要开始逻辑书写,我们的servelt的包名是:com.babybus.sdteam.bo.LoginServlet,这个类必须继承HttpServlet,然后重写doPost和doGet方法,分别处理GetPost的请求。

    package com.babybus.sdteam.bo;
    
    import java.io.IOException;
    
    import javax.persistence.metamodel.SetAttribute;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class LoginServlet extends HttpServlet{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	public void doGet(HttpServletRequest req,HttpServletResponse rep)
    	{
    		try {
    			process(req,rep);
    		} catch (ServletException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	public void doPost(HttpServletRequest req,HttpServletResponse rep)
    	{
    		try {
    			process(req,rep);
    		} catch (ServletException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	
    	/**
    	 * 业务处理
    	 * @param req
    	 * @param rep
    	 * @throws IOException 
    	 * @throws ServletException 
    	 */
    	public void process(HttpServletRequest req,HttpServletResponse rep) throws ServletException, IOException
    	{
    		// 根据页面上name获取相应的控件值
    		String username = req.getParameter("username");
    		String userid = req.getParameter("userid");
    		
    		// 逻辑处理,设置相应的结果字符串
    		String resultstring = "用户名:"+username+" ID:" + userid;
    		req.setAttribute("resultstring", resultstring);
    		
    		// 页面跳转
    		RequestDispatcher dispatcher = req.getRequestDispatcher("loginsuccess.jsp");
    		dispatcher.forward(req, rep); 
    	}
    }
    

     这样的话我们的登陆就做好了。最后看看loginsuccess.jsp怎么显示数据!

    <%=(String)request.getAttribute("resultstring")%>登陆成功

    没错,就是这么简单。

    结语

    • 受益,学会了如何编写一个简单的JSP+SERVLET程序

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4559684.html

  • 相关阅读:
    重新想象 Windows 8 Store Apps (32) 加密解密: 非对称算法, 数据转换的辅助类
    《C#编程极限》目录
    《软件设计精要与模式》完稿(原名《软件设计之道》)
    《软件设计之道》正式更名为《软件设计精要与模式》
    《软件设计精要与模式》各篇之篇首语
    Visual Studio 2005单元测试中关于外部文件的问题解决
    Web Service Software Factory
    Windows下IIS+PHP 5.2的安装与配置
    规划你的C#程序——《C#编程极限》第一章
    《软件设计精要与模式》前言
  • 原文地址:https://www.cnblogs.com/superdo/p/4559684.html
Copyright © 2020-2023  润新知