<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<marquee bgcolor="blue"><font size="10" color="blue">red</font></marquee><br />
<br>
<br>
<br>
<br>
<table align="center" bgcolor="#D7EEFF">
<tr>
<td align="left" width="250" height="100">
ログイン
ユーザー名およびパスワードを入力してOKをクリックし<br>てください。
</td>
</tr>
<tr>
<td align="center" width="250" height="100">
<form action="./login" method="post">
ユーザー名:<input type="text" name="name"><br>
パスワード:<input type="password" name="pass" value="1234"><br>
<input type="submit" value="OK">
</td>
</tr>
</table>
</form>
</body>
</html>
2014年10月22日水曜日
2014年10月21日火曜日
include+paramアクション
include+paramアクション
http://www.javaroad.jp/servletjsp/sj_jsp4.htm
http://www.javadrive.jp/servlet/jsp_action/index5.html
インクルードディレクティブとインクルードアクションの違い
http://www.atmarkit.co.jp/ait/articles/0107/31/news001.html
JSPでレイアウトのテンプレートを作る
http://qiita.com/Shun0750@github/items/a4e7487f091cfd42b91a
URLパターン(パスマッピング)
http://www.javadrive.jp/servlet/webxml/index6.html
http://www1.parkcity.ne.jp/hamaf/technical.html
コネクション・プーリング
http://www.wakhok.ac.jp/~tomoharu/web2004/text/index_c8.html#doc1_id1259
2014年10月19日日曜日
servlet jsp code
Login.java
====================================================
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
init(request,response);
/* // リクエストパラメータの取得
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
}
// ログイン結果画面にフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/loginResult.jsp");
dispatcher.forward(request, response);*/
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
init(request,response);
}
private void init(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
// リクエストパラメータの取得
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
request.setAttribute("flg", "1");
}
// ログイン結果画面にフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/loginResult.jsp");
dispatcher.forward(request, response);
}
}
====================================================
LoginLogic.java
import model.User;
public class LoginLogic {
public boolean execute(User user) {
if (user.getPass().equals("1234")) {
return true;
}
return false;
}
}
====================================================
Pagesend.java
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;
/**
* Servlet implementation class Pagesend
*/
public class Pagesend extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Pagesend() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
private void pageget (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=Shift_JIS");
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>テスト</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<p>");
sb.append("getRequestURL:");
sb.append(request.getParameter("url"));
sb.append("</p>");
/* sb.append("<p>");
sb.append("getRequestURI:");
sb.append(request.getRequestURI());
sb.append("</p>");*/
/* sb.append("<p>");
sb.append("getServletPath:");
sb.append(request.getServletPath());
sb.append("</p>");*/
sb.append("</body>");
sb.append("</html>");
out.println(new String(sb));
out.close();
}
}
====================================================
Prope2.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Prope
*/
public class Prope2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Prope2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response); }
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);}
private void pageget (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletContext context = this.getServletContext();
String path = context.getRealPath("/properties.txt");
// プロパティファイルをロード
InputStream in = new FileInputStream(path);
Properties properties = new Properties();
properties.load(in);
in.close();
// プロパティファイルパス+プロパティリスト表示
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.append("<html>");
out.append("<head>");
out.append("<title>テスト</title>");
out.append("</head>");
out.append("<body>");
out.println("key1=" + properties.getProperty("key1") + "<br>");
out.println("key1=" + properties.getProperty("key2") + "<br>");
out.println("key1=" + properties.getProperty("key3") + "<br>");
out.append("</body>");
out.append("</html>");
out.close();
/* String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
request.setAttribute("flg", "1");
}
*/ // ログイン結果画面にフォワード
/* RequestDispatcher dispatcher =
request.getRequestDispatcher
("link1.jsp");
dispatcher.forward(request, response); */
}
}
=====================================
Link1.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;
/**
* Servlet implementation class Pagesend
*/
public class Link1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Link1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
private void pageget (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// リクエストパラメータの取得
request.setCharacterEncoding("UTF-8");
String pagenum = request.getParameter("pagenum");
request.setAttribute("pagenum", pagenum);
/*
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>テスト</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<p>");
sb.append("Link1.java:pagenum:");
sb.append(pagenum);
sb.append("</p>");
sb.append("</body>");
sb.append("</html>");
out.println(new String(sb));
out.close(); */
/* String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
request.setAttribute("flg", "1");
}
*/ // ログイン結果画面にフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher
("link1.jsp");
dispatcher.forward(request, response);
}
}
===============================
User.java
package model;
import java.io.Serializable;
public class User implements Serializable {
private String name; // ユーザー名
private String pass; // パスワード
public User() {
}
public User(String name, String pass) {
this.name = name;
this.pass = pass;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
}
===============================
Login.jsp
<%-- リスト10-6の状態 --%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>どこつぶ</title>
</head>
<body>
<h1>どこつぶへようこそ</h1>
<form action="./login" method="post">
ユーザー名:<input type="text" name="name"><br>
パスワード:<input type="password" name="pass" value="1234"><br>
<input type="submit" value="ログイン">
</form>
</body>
</html>
===========================
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="model.User" %>
<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
<%
// セッションスコープからユーザー情報を取得
User loginUser = (User) session.getAttribute("loginUser");
%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" >
function PageSend(page){
//alert(page);
//document.frm.url.value=page;
document.frm1.action=page;
document.frm1.submit();
}
//function kakunin(){
// ret = confirm("Yahoo!Japanへ飛びます。宜しいですか?");
// if (ret == true){
// location.href = "http://www.yahoo.co.jp/";
// }
//}
</script>
</head>
</head>
<body>
<form method="post" action="" name="frm1">
<% String flg=(String)request.getAttribute("flg"); %>
<% if( flg.equals("1")) { %>
<!--
<a href="link1">リンク1</a>
<a href="link2">リンク2</a>
<a href="link3">リンク3</a>
<a href="link4">リンク4</a>
-->
<input type="button" name="link1" value="リンク1" onClick="PageSend('link1')">
<input type="button" value="リンク2" onClick="PageSend('link2')">
<input type="button" value="リンク3" onClick="PageSend('link3')">
<input type="button" value="リンク4" onClick="PageSend('link4')">
<!--
<input type="button" name="yahoo" value="Yahoo!Japanへ" onClick="kakunin(1)">
-->
<% } %>
<%
// 実行日を取得
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm a");
String today = sdf.format(date);
%>
<%= today %>
<% if( flg.equals("1")) { %>
<input type="button" value="ログアウト" onClick="location.href='Login.jsp'">
<% } %>
<% if( flg.equals("0")) { %>
<input type="button" value="閉じる" onClick="location.href='Login.jsp'">
<% } %>
<input type="text" name="pagenum" value= "1" >
</form>
</body>
</html>
<jsp:include page="/pagesend.jsp" />
===========================
pagesend.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<script type="text/javascript" >
function pagesend(page,num)
{
document.frm.url.value=page;
//document.frm.pagenum.value=Number(document.frm.pagenum.value)+num;
//if(13 != code)
document.frm.nowpgnum.value=Number(document.frm.nowpgnum.value)+num;
alert(num);
// }
}
</script>
</head>
<body>
<form method="post" action="pagesend" name="frm">
<input type="submit" value="<" onClick="pagesend('<%= request.getRequestURI() %>',-1,0);">
<input type="text" name="nowpgnum" value= "" onkeypress="pagesend('<%= request.getRequestURI() %>',0);">
<input type="submit" value=">" onClick="pagesend('<%= request.getRequestURI() %>',1,0);">
<input type="text" name="url" value= "" >
<input type="text" name="pagenum" value= "" >
<!--
<%= request.getRequestURI() %>
-->
</form>
</body>
</html>
=========================
link1.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>インクルードのサンプル</title>
</head>
<body>
<jsp:include page="/pagesend.jsp" />
<h1>link1</h1>
<p>「どこつぶ」は・・・</p>
<% String pagenum=(String)request.getAttribute("pagenum"); %>
Pagenum:
<%= pagenum %>
</body>
</html>
===========================
link2.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>link2</title>
</head>
<body>
<jsp:include page="/loginResult.jsp" />
<h1>link2</h1>
<p>「どこつぶ」は・・・</p>
<jsp:include page="/footer.jsp" />
</body>
</html>
============================
link3.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>link2</title>
</head>
<body>
<jsp:include page="/loginResult.jsp" />
<h1>link3</h1>
<p>「どこつぶ」は・・・</p>
<jsp:include page="/footer.jsp" />
</body>
</html>
==========================
link4.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>link2</title>
</head>
<body>
<jsp:include page="/loginResult.jsp" />
<h1>link4</h1>
<p>「どこつぶ」は・・・</p>
<jsp:include page="/footer.jsp" />
</body>
</html>
================================
properties.txt
key1=1
key2=2
key3=3
=======================
====================================================
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
init(request,response);
/* // リクエストパラメータの取得
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
}
// ログイン結果画面にフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/loginResult.jsp");
dispatcher.forward(request, response);*/
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
init(request,response);
}
private void init(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
// リクエストパラメータの取得
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
request.setAttribute("flg", "1");
}
// ログイン結果画面にフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/loginResult.jsp");
dispatcher.forward(request, response);
}
}
====================================================
LoginLogic.java
import model.User;
public class LoginLogic {
public boolean execute(User user) {
if (user.getPass().equals("1234")) {
return true;
}
return false;
}
}
====================================================
Pagesend.java
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;
/**
* Servlet implementation class Pagesend
*/
public class Pagesend extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Pagesend() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
private void pageget (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=Shift_JIS");
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>テスト</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<p>");
sb.append("getRequestURL:");
sb.append(request.getParameter("url"));
sb.append("</p>");
/* sb.append("<p>");
sb.append("getRequestURI:");
sb.append(request.getRequestURI());
sb.append("</p>");*/
/* sb.append("<p>");
sb.append("getServletPath:");
sb.append(request.getServletPath());
sb.append("</p>");*/
sb.append("</body>");
sb.append("</html>");
out.println(new String(sb));
out.close();
}
}
====================================================
Prope2.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Prope
*/
public class Prope2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Prope2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response); }
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);}
private void pageget (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletContext context = this.getServletContext();
String path = context.getRealPath("/properties.txt");
// プロパティファイルをロード
InputStream in = new FileInputStream(path);
Properties properties = new Properties();
properties.load(in);
in.close();
// プロパティファイルパス+プロパティリスト表示
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.append("<html>");
out.append("<head>");
out.append("<title>テスト</title>");
out.append("</head>");
out.append("<body>");
out.println("key1=" + properties.getProperty("key1") + "<br>");
out.println("key1=" + properties.getProperty("key2") + "<br>");
out.println("key1=" + properties.getProperty("key3") + "<br>");
out.append("</body>");
out.append("</html>");
out.close();
/* String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
request.setAttribute("flg", "1");
}
*/ // ログイン結果画面にフォワード
/* RequestDispatcher dispatcher =
request.getRequestDispatcher
("link1.jsp");
dispatcher.forward(request, response); */
}
}
=====================================
Link1.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;
/**
* Servlet implementation class Pagesend
*/
public class Link1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Link1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
pageget (request,response);
}
private void pageget (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// リクエストパラメータの取得
request.setCharacterEncoding("UTF-8");
String pagenum = request.getParameter("pagenum");
request.setAttribute("pagenum", pagenum);
/*
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>テスト</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<p>");
sb.append("Link1.java:pagenum:");
sb.append(pagenum);
sb.append("</p>");
sb.append("</body>");
sb.append("</html>");
out.println(new String(sb));
out.close(); */
/* String pass = request.getParameter("pass");
// Userインスタンス(ユーザー情報)の生成
User user = new User(name, pass);
// ログイン処理
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
// ログイン成功時の処理
if (isLogin) {
// ユーザー情報をセッションスコープに保存
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
request.setAttribute("flg", "1");
}
*/ // ログイン結果画面にフォワード
RequestDispatcher dispatcher =
request.getRequestDispatcher
("link1.jsp");
dispatcher.forward(request, response);
}
}
===============================
User.java
package model;
import java.io.Serializable;
public class User implements Serializable {
private String name; // ユーザー名
private String pass; // パスワード
public User() {
}
public User(String name, String pass) {
this.name = name;
this.pass = pass;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
}
===============================
Login.jsp
<%-- リスト10-6の状態 --%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>どこつぶ</title>
</head>
<body>
<h1>どこつぶへようこそ</h1>
<form action="./login" method="post">
ユーザー名:<input type="text" name="name"><br>
パスワード:<input type="password" name="pass" value="1234"><br>
<input type="submit" value="ログイン">
</form>
</body>
</html>
===========================
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="model.User" %>
<%@ page import="java.util.Date,java.text.SimpleDateFormat" %>
<%
// セッションスコープからユーザー情報を取得
User loginUser = (User) session.getAttribute("loginUser");
%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" >
function PageSend(page){
//alert(page);
//document.frm.url.value=page;
document.frm1.action=page;
document.frm1.submit();
}
//function kakunin(){
// ret = confirm("Yahoo!Japanへ飛びます。宜しいですか?");
// if (ret == true){
// location.href = "http://www.yahoo.co.jp/";
// }
//}
</script>
</head>
</head>
<body>
<form method="post" action="" name="frm1">
<% String flg=(String)request.getAttribute("flg"); %>
<% if( flg.equals("1")) { %>
<!--
<a href="link1">リンク1</a>
<a href="link2">リンク2</a>
<a href="link3">リンク3</a>
<a href="link4">リンク4</a>
-->
<input type="button" name="link1" value="リンク1" onClick="PageSend('link1')">
<input type="button" value="リンク2" onClick="PageSend('link2')">
<input type="button" value="リンク3" onClick="PageSend('link3')">
<input type="button" value="リンク4" onClick="PageSend('link4')">
<!--
<input type="button" name="yahoo" value="Yahoo!Japanへ" onClick="kakunin(1)">
-->
<% } %>
<%
// 実行日を取得
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm a");
String today = sdf.format(date);
%>
<%= today %>
<% if( flg.equals("1")) { %>
<input type="button" value="ログアウト" onClick="location.href='Login.jsp'">
<% } %>
<% if( flg.equals("0")) { %>
<input type="button" value="閉じる" onClick="location.href='Login.jsp'">
<% } %>
<input type="text" name="pagenum" value= "1" >
</form>
</body>
</html>
<jsp:include page="/pagesend.jsp" />
===========================
pagesend.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<script type="text/javascript" >
function pagesend(page,num)
{
document.frm.url.value=page;
//document.frm.pagenum.value=Number(document.frm.pagenum.value)+num;
//if(13 != code)
document.frm.nowpgnum.value=Number(document.frm.nowpgnum.value)+num;
alert(num);
// }
}
</script>
</head>
<body>
<form method="post" action="pagesend" name="frm">
<input type="submit" value="<" onClick="pagesend('<%= request.getRequestURI() %>',-1,0);">
<input type="text" name="nowpgnum" value= "" onkeypress="pagesend('<%= request.getRequestURI() %>',0);">
<input type="submit" value=">" onClick="pagesend('<%= request.getRequestURI() %>',1,0);">
<input type="text" name="url" value= "" >
<input type="text" name="pagenum" value= "" >
<!--
<%= request.getRequestURI() %>
-->
</form>
</body>
</html>
=========================
link1.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>インクルードのサンプル</title>
</head>
<body>
<jsp:include page="/pagesend.jsp" />
<h1>link1</h1>
<p>「どこつぶ」は・・・</p>
<% String pagenum=(String)request.getAttribute("pagenum"); %>
Pagenum:
<%= pagenum %>
</body>
</html>
===========================
link2.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>link2</title>
</head>
<body>
<jsp:include page="/loginResult.jsp" />
<h1>link2</h1>
<p>「どこつぶ」は・・・</p>
<jsp:include page="/footer.jsp" />
</body>
</html>
============================
link3.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>link2</title>
</head>
<body>
<jsp:include page="/loginResult.jsp" />
<h1>link3</h1>
<p>「どこつぶ」は・・・</p>
<jsp:include page="/footer.jsp" />
</body>
</html>
==========================
link4.jsp
<%@ page language="java" contentType="text/html; charset=shift_jis" pageEncoding="shift_jis" %>
<!DOCTYPE html>
<html>
<head>
<title>link2</title>
</head>
<body>
<jsp:include page="/loginResult.jsp" />
<h1>link4</h1>
<p>「どこつぶ」は・・・</p>
<jsp:include page="/footer.jsp" />
</body>
</html>
================================
properties.txt
key1=1
key2=2
key3=3
=======================
2014年10月14日火曜日
登録:
投稿 (Atom)