JAVAEE-JSP

JAVAEE-JSP
一:新建javaweb工程
ps:可能有的编译器新建之后,是以下样式:
这里写图片描述
就是在WEB-INF下面没有web.xml
web.xml实例:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>d</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

也可以直接在新建的时候,勾选一下,即可自动创建
这里写图片描述

二:创建login实例

1-1:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>2017-06-27loginTest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>login</servlet-name>
  	<servlet-class>com.control.LoginPage</servlet-class>
  </servlet>
  <servlet>
  	<servlet-name>logincheck</servlet-name>
  	<servlet-class>com.control.LoginCheck</servlet-class>
  </servlet>
  <servlet>
  	<servlet-name>index</servlet-name>
  	<servlet-class>com.control.indexPage</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>login</servlet-name>
  	<url-pattern>/login.action</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>logincheck</servlet-name>
  	<url-pattern>/loginCheck.action</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>index</servlet-name>
  	<url-pattern>/indexPage.action</url-pattern>
  </servlet-mapping>
</web-app>


login
//serlvet-name:servlet的名称
com.control.LoginPage
//serlvet-class:servlet对应的class,包名之间用.隔开

login //serlvet-name:servlet的名称 /login.action //url-pattern:servlet跳转的路径url **重点:**两个servlet-name要相同

1-2:login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
<%
	String path = request.getContextPath();
    String msg=request.getParameter("msg");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="<%=path%>/loginCheck.action">
		账号:<input type="text" name="userId"> <br/>
		密码:<input type="text"  name="pwd"><br/>
		<button type="submit">提交</button>
	</form>
</body>
</html>

1-3:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
     String id=request.getParameter("userId");
     String pwd=request.getParameter("pwd");
     String msg=(String)request.getAttribute("msg");
     System.out.println("==user:"+id+":"+"pwd:"+pwd+" msg:"+msg);
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
你好:<%=id %>,<%=pwd %>,<%=msg %>
</body>
</html>

对应的后端java处理代码:
2-1:LoginPage.java

package com.control;

import java.io.IOException;

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

public class LoginPage extends HttpServlet {
 @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	 req.setAttribute("msg", "下大雨了");
	 req.getRequestDispatcher("/login.jsp").forward(req, resp);
    }
}

2-2:LoginCheck.java

package com.control;

import java.io.IOException;

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

public class LoginCheck extends HttpServlet {
   @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String id=req.getParameter("userId");
       String pwd=req.getParameter("pwd");
       System.out.println("id:"+id);
       System.out.println("pwd:"+pwd);
       if(id.trim().equals("123")&&pwd.trim().equals("222"))
       {  
    	   pwd=pwd+id;
    	   System.out.println("里面的id:"+id);
    	   System.out.println("里面的pwd:"+pwd);
    	   req.setAttribute("user", id);
    	   req.setAttribute("pwd", pwd);
    	   req.getRequestDispatcher("/indexPage.action").forward(req, resp);
       }else
       {
          req.getRequestDispatcher("/login.action").forward(req, resp);
       }
  }
}

2-3:indexPage.java

package com.control;

import java.io.IOException;

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

public class indexPage extends HttpServlet {
 @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	 req.setAttribute("msg", "123");
	 req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值