20
LING 408/508: Programming for Linguists Lecture 17 October 21 st

LING 408/508: Programming for Linguists Lecture 17 October 21 st

Embed Size (px)

Citation preview

Page 1: LING 408/508: Programming for Linguists Lecture 17 October 21 st

LING 408/508: Programming for Linguists

Lecture 17October 21st

Page 2: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Administrivia

• No class all next week

Page 3: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Last Time

• We enabled our first Web server:– sudo apachectl –k start

• Do we all have our webservers up and running?

Page 4: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Last Time• Client: ~sandiway/Desktop/form.html • Server: /Library/Webserver/CGI-

Executables/program.cgi

Content-type: text/plain

http://localhost/cgi-bin/program.cgi?first=Sandiway&last=Fong

$QUERY_STRING

sudo chmod 755program.cgi

Page 5: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Sending information: GET

• program.cgi:#!/bin/bashecho "Content-type: text/plain"echo#echo $QUERY_STRINGorigIFS=$IFSIFS='=&'set -- $QUERY_STRINGIFS=$origIFSecho "1:<$1> 2:<$2> 3:<$3> 4:<$4>"

In bash:IFS = internal field separator (for arguments)default: space newline tabset – String -- option: positional parameters are set after parsing String

Page 6: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Sending information: POST

• HTML form:<form action="http://localhost/cgi-bin/read.cgi" method="POST"> First: <input type="text" name="first" size=12>Last: <input type="text" name="last" size=12><input type="submit"></form>

• bash accesses the URL-encoded string on standard input via read

Page 7: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Sending information: POST

• bash accesses the URL-encoded string on standard input via read

• Example read.cgi:#!/bin/bashecho "Content-Type: text/plain"echoread inputorigIFS=$IFSIFS='=&'set -- $inputIFS=$origIFSecho "<$2><$4>"

Page 8: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Other webservers

• Perl module• PHP module• Tomcat (Java)

Page 9: LING 408/508: Programming for Linguists Lecture 17 October 21 st

mod_perl

• Apache webserver module with Perl– not enabled by default• /etc/apache2/httpd.conf (on OSX)• #LoadModule perl_module libexec/apache2/mod_perl.so

• Add to httpd.conf:– PerlModule Apache2::Status– <Location /perl-status>– SetHandler perl-script– PerlHandler Apache2::Status– </Location>

Page 10: LING 408/508: Programming for Linguists Lecture 17 October 21 st

mod_perlhttp://localhost/perl-status

Page 11: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Registry Scripts

Add to httpd.conf:• <Location /perl/>• SetHandler perl-script• PerlResponseHandler ModPerl::Registry• PerlOptions +ParseHeaders• Options +ExecCGI• Order allow,deny• Allow from all • </Location>

Page 12: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Registry Scripts

perl/test.pl (/Library/WebServer/Documents/)• #!/usr/bin/perl• print "Content-type: text/plain\n\n";• print "Test Perl program works!\n";URL:• http://localhost/perl/test.pl

Page 13: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Other webservers

• Perl module• PHP module• Tomcat (Java)

Page 14: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Tomcat• JSP = JavaServer Pages (HTML with embedded Java code)• Apache Tomcat: a JSP webserver from Apache

– http://tomcat.apache.org (8.0)• Install:

– /usr/local/apache-tomcat-8.0.12/• Subdirectories:

– webapps/ (put app .war files here – like a bundle)– webapps/ROOT/ (.jsp .html files can go directly here too)– logs/catalina.out (log output from java, – e.g. print statements)– lib/ (common .class files can go here)– bin/startup.sh (start webserver)– bin/shutdown.sh (stop webserver)

Page 15: LING 408/508: Programming for Linguists Lecture 17 October 21 st

class files

• Class Loader HOW-TO– common• unpacked classes and resources in $CATALINA_BASE/lib

– http://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html

Page 16: LING 408/508: Programming for Linguists Lecture 17 October 21 st

mixing Java and html• Tags:

– <jsp:include page="hello.jsp"/>• Scriptlets:

– <% … %> Java code– <%= … %> evaluate expression– <%! … %> declarations– <%@ include file=".." %> include directive– <%@ page … %> page directive, e.g. import="java.util.*"Predefined variables:– out.println()– request.getRemoteHost()– response.sendRedirect()

Page 17: LING 408/508: Programming for Linguists Lecture 17 October 21 st

forms and sessions

Webpage (client side):• <form action="process.jsp">• <INPUT TYPE=TEXT NAME=username>• </form>

process.jsp (server side):• <% String name = request.getParameter( "username" );• session.setAttribute( "yourName", name ); %>

Some other .jsp file (server side):• <%= session.getAttribute( "yourName" ) %>

Page 18: LING 408/508: Programming for Linguists Lecture 17 October 21 st

beans• class with methods for setting and getting field names of formsForm field:• <INPUT TYPE=TEXT NAME=username>Define a class with standardized naming:• public class UserData {• String username;

• public void setUsername( String value )• {• username = value;• }• public String getUsername() { return username; }• }Storage:• <jsp:useBean id="user" class="user.UserData" scope="session"/>• <jsp:setProperty name="user" property="*"/> Retrieval:• <jsp:useBean id="user" class="user.UserData" scope="session"/> • <%= user.getUsername() %>

Page 19: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Example

• Stanford Parser:

Page 20: LING 408/508: Programming for Linguists Lecture 17 October 21 st

Homework 5

• Use cgi-bin to implement your BMI shell script

• Use both GET and POST methods

• Send me your code next week• Hard to evaluate:– Demonstrate in it class