Posts Tagged ‘servlet questions’

COBOL FAQ

Wednesday, March 25th, 2009

1. What is a reentrant program? How will you make a program Reentrant?

A. A reentrant program, while being executed resides in the common virtual area so that one copy of it may be shared among all callers. Use RENT compiler option.

2. What is the difference between a 01 level and 77 level?

A. 01 level can have sublevels from 02 to 49. 77 cannot have sublevel.

3. What are the few advantages of VS COBOL II over OS/VS COBOL?

A. The working storage and linkage section limit has been increased. They are 128 megabytes as supposed to 1 megabytes in OS/VS COBOL. Introduction of ADDRESS special register.

31-bit addressing. In using COBOL on PC we have only flat files and the programs can access only limited storage, whereas in VS COBOL II on M/F the programs can access up to 16MB or 2GB depending on the addressing and can use VSAM files to make I/O operations faster.

4. What are the steps you go through while creating a COBOL program executable?

A. DB2 pre-compiler (if embedded SQL is used), CICS translator (if CICS program), Cobol compiler, Link editor. If DB2 program, create plan by binding the DBRMs.

5. What are the minimum requirements to compile a program without errors?

A. Identification Division.

Program-ID. Program-name.

A) Is compute w=u a valid statement?

Yes, it is. It is equivalent to move u to w.

B) In the above example, when will you prefer compute statement over the move statement?

When significant left-order digits would be lost in execution, the COMPUTE statement can detect the condition and allow you to handle it. The MOVE statement carries out the assignment with destructive truncation. Therefore, if the size error is needs to be detected, COMPUTE will be preferred over MOVE. The ON SIZE ERROR phrase of COMPUTE statement, compiler generates code to detect size-overflow.

6. What happens when the ON SIZE ERROR phrase is specified on a COMPUTE statement?

A. If the condition occurs, the code in the ON SIZE ERROR phrase is performed, and the content of the destination field remains unchanged. If the ON SIZE ERROR phrase is not specified, the assignment is carried out with truncation. There is no ON SIZE ERROR support for the MOVE statement.

7. How will you associate your files with external data sets where they physically reside?

A. Using SELECT clause, the files can be associated with external data sets. The SELECT clause is defined in the FILE-CONTROL paragraph of Input-Output Section that is coded Environment Division. The File structure is defined by FD entry under File-Section of Data Division for the OS.

8. How will you define your file to the operating system?

A. Associate the file with the external data set using SELECT clause of INPUT-OUTPUT SECTION. INPUT-OUTPUT SECTION appears inside the ENVIRONMENT DIVISION.

Define your file structure in the FILE SECTION of DATA DIVISION.

9. Explain the use of Declaratives in COBOL?

  1. Declaratives provide special section that are executed when an exceptional condition occurs. They must be grouped together and coded at the beginning of procedure division and the entire procedure division must be divided into sections. The Declaratives start with a USE statement. The entire group of declaratives is preceded by DECLARIVES and followed by END DECLARITIVES in area A. The three types of declaratives are Exception (when error occurs during file handling), Debugging (to debug lines with ‘D’ coded in w-s section) and Label (for EOF or beginning…) declaratives.

10. A statically bound subprogram is called twice. What happens to working-storage variables?

A. The working-storage section is allocated at the start of the run-unit and any data items with VALUE clauses are initialized to the appropriate value at the time. When the subprogram is called the second time, a working-storage items persist in their last used state. However, if the program is specified with INITIAL on the PROGRAM-ID, working-storage section is reinitialized each time the program is entered.

PROGRAM-ID. <Pgmname> is INITIAL PROGRAM. Other verbs used with PROGRAM-ID are RECURSIVE and COMMON.

11. When is COMMON attribute used?

A. COMMON attribute is used with nested COBOL programs. If it is not specified, other nested programs will not be able to access the program. PROGRAM-ID. Pgmname is COMMON PROGRAM.

12. In which division and section, the Special-names paragraph appears?

A. Environment division and Configuration Section.

13. What is the LOCAL-STORAGE SECTION?

A. Local-Storage is allocated each time the program is called and is de-allocated when the program returns via an EXIT PROGRAM, GOBACK, or STOP RUN. Any data items with a VALUE clauses are initialized to the appropriate value each time the program is called. The value in the data items is lost when the program returns. It is defined in the DATA DIVISION after WORKING-STORAGE SECTION

14. What does passing BY REFERENCE mean?

A. When the data is passed between programs, the subprogram refers to and processes the data items in the calling program’s storage, rather than working on a copy of the data. When

CALL . . . BY REFERENCE identifier. In this case, the caller and the called share the same memory.

15. What does passing BY CONTENT mean?

A. The calling program passes only the contents of the literal, or identifier. With a CALL . . . BY CONTENT, the called program cannot change the value of the literal or identifier in the calling program, even if it modifies the variable in which it received the literal or identifier.

16. What does passing BY VALUE mean?

A. The calling program or method is passing the value of the literal, or identifier, not a reference to the sending data item. The called program or invoked method can change the parameter in the called program or invoked method. However, because the subprogram or method has access only to a temporary copy of the sending data item, those changes do not affect the argument in the calling program. Use By value, If you want to pass data to C program. Parameters must be of certain data type.

17. What is the default, passing BY REFERENCE or passing BY CONTENT or passing BY VALUE?

A. Passing by reference (the caller and the called share the same memory).

18. Where do you define your data in a program if the data is passed to the program from a Caller program?

A. Linkage Section

19. Define the structure of a COBOL subroutine.

A. The PROCEDURE DIVISION header must have a using a phrase, if the data needs to be passed to the program. The operands of the USING phrase must be defined in the LINKAGE SECTION as 01-level or 77-level entries. No VALUE clause is allowed unless the data defined is a condition name.

If the program needs to be returned to the CALLER, use EXIT PROGRAM statement at the end of the program. GOBACK is an alternative, but is nonstandard.

20. What is difference between next sentence and continue

A. NEXT SENTENCE gives control to the verb following the next period. CONTINUE gives control to the next verb after the explicit scope terminator. (This is not one of COBOL II’s finer implementations). It’s safest to use CONTINUE rather than NEXT SENTENCE in COBOL II. CONTINUE is like a null statement (do nothing) , while NEXT SENTENCE transfers control to the next sentence (!!) (A sentence is terminated by a period). Check out by writing the following code example, one if sentence followed by 3 display statements: If 1 > 0 then next sentence end if display ‘line 1′ display ‘line 2′. display ‘line 3′. *** Note- there is a dot (.) only at the end of the last 2 statements, see the effect by replacing Next Sentence with Continue ***

21. What is the difference between Structured Cobol Programming and Object Oriented COBOL programming?

A. Structured programming is a Logical way of programming using which you divide the functionality’s into modules and code logically. OOP is a Natural way of programming in which you identify the objects, first then write functions and procedures around the objects. Sorry, this may not be an adequate answer, but they are two different programming paradigms, which is difficult to put in a sentence or two.

22. PIC S9(4)COMP IS USED INPSPITE OF COMP-3 WHICH OCCUPIES LESS SPACE.WHY?

A. S9(4) COMP uses only 2 bytes. 9(4) COMP-3 uses 3 bytes. 3 bytes are more than 2 bytes. Hence COMP is preferred over COMP-3 in this case.

23. How many number of bytes and digits are involved in S9(10) COMP?

  1. 8 bytes (double word) and 10 digits. Up to 9(9) comp use full word, up to 9(18) comp needs double word.

24. Which picture clause will you use to define a hexadecimal item in a VALUE clause?

A. 01 ws-hexitem PIC X(2) value X’020C’.

01 ws-hex redefines PIC S9(3) comp-3.

25. How many numbers of decimal digits are possible, when the amount of storage allocated for a USAGE COMP item is a) half word b) full word c) double word?

A. 2 bytes (halfword) for 1-4 Digits 4 bytes (fullword) for 5-9

8 bytes (doubleword) for 10-18

26. What is a scope terminator? Give examples.

  1. Scope terminator is used to mark the end of a verb e.g. EVALUATE, END-EVALUATE; IF, END-IF.

27. How many dimensions are allowed for a table?

A. 7

28. How many subscripts or indexes are allowed for an OCCURS clause?

A. 7

29. Why cannot Occurs be used in 01 level?

A. Because, Occurs clause is there to repeat fields with the same format, but not the records.

30. Can a REDEFINES clause be used along with an OCCURS clause?

A. Yes, if the REDEFINES clause is subordinate to OCCURS clause.

31. Can you specify PIC clause and a VALUE with an OCCURS clause? Will the following code compile without errors?

01 WS-TABLE.

03 WS-TABLE-EL OCCURS 5 TIMES PIC X(1) VALUE SPACES.

A. Yes, the code will compile without any errors.

32. What would be the output, when the following code is executed?

01 WS-TABLE.

03 WS-TABLE-EL OCCURS 5 TIMES PIC X(1) VALUE ‘AAAAA’.

A. It cannot be executed because the code will compile with error ‘ “VALUE” literal “‘AAAA’” exceeded the length specified in the “PICTURE” definition’.

33. 01 WS-TABLE.

03 WS-TABLE-EL OCCURS 5 TIMES PIC X(1) VALUE ‘A’.

03 WS-EX REDEFINES WS-TABLE-EL PIC X(5). What can you expect?

A. Compile error. Direct Redefinition of OCCURS clause is not allowed.

34. 01 WS-TABLE.

03 WS-TABLE-EL OCCURS 5 TIMES.

04 FILLER-X PIC X(1) VALUE ‘A’. 04 WS-EX REDEFINES FILLER-X PIC X(1).

What would be the output of DISPLAY WS-TABLE?

A. ‘AAAAA’. The code will compile and execute as Redefinition of an item subordinate to OCCURS clause.

35. Is this allowed?

01 WS-TABLE.

03 FILLER-X PIC X(5) VALUE ‘AAAAA’.

03 WS-EX REDEFINES FILLER-X.

04 WS-TABLE-EL OCCURS 5 TIMES PIC X(1).

A. Yes

35. Is this allowed?

01 WS-TABLE.

03 FILLER-X PIC X(5) VALUE ‘AAAAA’.

03 WS-EX REDEFINES FILLER-X OCCURS 5 TIMES PIC X(1).

A. Yes

36. Which SEARCH verb is equivalent to PERFORM…VARYING?

  1. The serial SEARCH verb (SEARCH without ALL)

37. What would be the output, when the following code is executed?

01 WS-TABLE.

03 WS-TABLE-EL OCCURS 5 TIMES PIC X(1) VALUE ‘A’.

:::

DISPLAY WS-TABLE.

A. The output will display ‘AAAAA’

38. Can a SEARCH be applied to a table which does not have an INDEX defined?

A. No, the table must be indexed.

39. What are the different rules applicable to perform a serial SEARCH?

A. The SEARCH can be applied to only a table which has the OCCURS clause and INDEXED BY phrase,

Before the use of the SEARCH the index must have some initial value. To search from beginning, set the index value to 1. Use the SEARCH verb without ALL phrase

40. A table has two indexes defined. Which one will be used by the SEARCH verb?

A. The index named first will be used, by Search.

41. What are the different rules applicable to perform a binary SEARCH?

A. The table must be sorted in ascending or descending order before the beginning of the SEARCH. Use OCCURS clause with ASC/DESC KEY IS dataname1 option

The table must be indexed. There is no need to set the index value. Use SEARCH ALL verb

42. How does the binary search work?

A. First the table is split into two halves and in which half, the item need to be searched is determined. The half to which the desired item may belong is again divided into two halves and the previous procedure is followed. This continues until the item is found. SEARCH ALL is efficient for tables larger than 70 items.

43. What is the difference between a binary search and a sequential search? What are the pertinent COBOL commands?

A. In a binary search the table element key values must be in ascending or descending sequence. The table is ‘halved’ to search for equal to, greater than or less than conditions until the element is found. In a sequential search the table is searched from top to bottom, so (ironically) the elements do not have to be in a specific sequence. The binary search is much faster for larger tables, While sequential Search works well with smaller ones. SEARCH ALL is used for binary searches; SEARCH for sequential.

44. Explain the difference between an internal and an external sort. The pros & cons & internal sort syntax …

A. An external sort is not coded as a COBOL program; it is performed through JCL and PGM=SORT. One can use IBM utility SYNCSORT for external sort process. It is understandable without any code reference. An internal sort can use two different syntaxes: 1.) USING, GIVING sorts are comparable to external sorts with no extra file processing; 2) INPUT PROCEDURE, OUTPUT PROCEDURE sorts allow for data manipulation before and/or after the sort. Syntax:

  • SORT file-1 ON ASCENDING/DESCENDING KEY key…USING file-2 GIVING file-3.
  • USING can be substituted by INPUT PROCEDURE IS para-1 THRU para-2
  • GIVING can be substituted by OUTPUT PROCEDURE IS para-1 THRU para-2.
  • file-1 is the sort workfile and must be described using SD entry in FILE SECTION.
  • file-2 is the input file for the SORT and must be described using an FD entry in FILE SECTION and SELECT clause in FILE CONTROL.
  • file-3 is the outfile from the SORT and must be described using an FD entry in FILE SECTION and SELECT clause in FILE CONTROL.
  • file-1, file-2 & file-3 should not be opened explicitly.
  • INPUT PROCEDURE is executed before the sort and records must be RELEASEd to the sort work file from the input procedure.
  • OUTPUT PROCEDURE is executed after all records have been sorted. Records from the sort work file must be RETURNed one at a time to the output procedure.
  • .How do you define a sort file in JCL that runs the COBOL program?
  • Use the SORTWK01, SORTWK02,….. dd names in the step. Number of sort datasets depends on the volume of data being sorted, but a minimum of 3 is required.

45. Which is the default, TEST BEFORE or TEST AFTER for a PERFORM statement?

  1. TEST BEFORE. By default the condition is checked before executing the instructions under Perform.

46. What is the difference between PERFORM … WITH TEST AFTER and PERFORM … WITH TEST BEFORE?

A. If TEST BEFORE is specified, the condition is tested at the beginning of each repeated execution of the specified PERFORM range. If TEST AFTER is specified, the condition is tested at the end of the each repeated execution of the PERFORM range. With TEST AFTER, the range is executed at least once.

47. How do you code an in-line PERFORM?

A. PERFORM … <UNTIL> … <statements> END-PERFORM.

48. In an EVALUTE statement is the order of the WHEN clauses significant?

  1. Yes. Evaluation of the WHEN clauses proceeds from top to bottom and their sequence can determine results.

49. What is the default value(s) for an INITIALIZE and what keyword allows for an override of the default.

A. INITIALIZE sets spaces to alphabetic and alphanumeric fields. Initialize sets Zeroes to numeric fields. FILLER, OCCURS DEPENDING ON items are left untouched. The REPLACING option can be used to override these defaults.

50. What is SET <condition-name> TO TRUE all about, anyway?

A. In COBOL II the 88 levels can be set rather than moving their associated values to the related data item. (Web note: This change is not one of COBOL II’s better specifications.)

51. What is LENGTH in COBOL II?

A. LENGTH acts like a special register to tell the length of a group or an elementary item.

52. What is the function of a delimiter in STRING?

A. A delimiter in STRING causes a sending field to be ended and another to be started.

53. What is the function of a delimiter in UNSTRING?

A. A delimiter when encountered in the sending field causes the current receiving field to be switched to the next one indicated.

54. How will you count the number of characters in a null-terminated string?

A. MOVE 0 TO char-count

INSPECT null-terminated-string TALLYING char-count FOR CHARACTERS BEFORE X”00″

55. Which statement will you use to move non-null characters from a null-terminated String?

A. UNSTRING null-terminated-string DELIMITED BY X”00″ INTO target-area

COUNT IN char-count. (There are other methods, such as 1) using PERFORM 2) using SEARCH 3) using INSPECT and MOVE etc…)

56. 77 COUNTR PIC 9 VALUE ZERO.

01 DATA-2 PIC X(11). . .

INSPECT DATA-2

TALLYING COUNTR FOR LEADING “0″

REPLACING FIRST “A” BY “2″ AFTER INITIAL “C”

If DATA-2 is 0000ALABAMA, what will DATA-2 and COUNTER be after the execution of INSPECT verb?

A.       Counter=4.             Data-2 will not change as the Initial 'C' is not found.

57. 01 DATA-4 PIC X(11).

:::

INSPECT DATA-4 CONVERTING

“abcdefghijklmnopqrstuvwxyz” TO “ABCDEFGHIJKLMNOPQRSTUVWXYZ”

AFTER INITIAL “/” BEFORE INITIAL”?”

What will the contents of DATA-4 be after the conversion statement is performed, if before conversion

a) DATA-4 = a/five/?six b) DATA-4 = r/Rexx/RRRr c) DATA-4 = zfour?inspe

A. a) a/FIVE/?six b) r/REXX/RRRR c) zfour?inspe (no change at all)

58. What kind of error is trapped by ON SIZE ERROR option?

A. Fixed-point overflow. Zero raised to the zero power.

Division by 0. Zero raised to a negative number.

A negative number raised to a fractional power.

 59. What is the point of the REPLACING option of a copy statement?

A. REPLACING allows for the same copy to be used more than once in the same code by changing the replace value. COPY xxx REPLACING <psuedotext1> BY <psuedotext2>.

60. When is a scope terminator mandatory?

A. Scope terminators are mandatory for in-line PERFORMS and EVALUATE statements. For readability, it’s recommended coding practice to always make scope terminators explicit.

61. Can you use REDEFINES clause in the FILE SECTION?

A. No

62. How will you define your record descriptions in the FILE SECTION if you want to use three different record descriptions for the same file?

A. FD filename

DATA RECORDS ARE rd01, rd02, rd03.

01 rd01 PIC X(n).

01 rd02 PIC X(n).

01 rd03 PIC X(n).

63. When will you open a file in the EXTEND mode?

A. When an existing file should be appended by adding new records at its end. EXTEND mode opens the file for output, but the file is positioned following the last record on the existing file.

64. What does a CLOSE WITH LOCK statement do?

A. The statement closes an opened file and it prevents the file from further being opened by the same program.

65. Which mode of opening is required when REWRITE is used?

A. I-O mode

66. Why is it necessary that the file be opened in I-O mode for REWRITE?

A. Before the REWRITE is performed, the record must be read from the file. Hence REWRITE includes an input operation and an output operation. Therefore, the file must be opened in I-O mode.

67. Which clause can be used instead of checking for FILE STATUS = 10?

A. FILE STATUS 10 is the end of file condition. Hence AT END clause can be used.

68. What is the format of a simple SORT verb? What kinds of files can be sorted using SORT?

A. SORT workfile ON ASC/DESC KEY key1, ASC/DESC KEY key2 …

USING inputfile GIVING outputfile

Only sequential files can be sorted in this way.

69. What are the different rules of SORT that needs to be considered?

  1. The input and output files must remain closed because SORT opens them and closes during the operation, The work file must have a SELECT clause. The work file must have sort description SD entry in the FILE SECTION. Input and Output files must have FD entries

70. What are INPUT PROCEDURE and OUTPUT PROCEDURE?

A. Sometimes, it is necessary that the records must be edited before or after the sorting. In such cases,

SORT workfile ASC/DESC KEY key1, …

INPUT PROCEDURE IS ipproc

OUTPUT PROCEDURE is outproc

Is used. In the INPUT PROCEDURE the input file is opened, records are read and edited and then are released to the sorting operation. Finally the file is closed. RELEASE sortrecname FROM inp-rec.

In the OUTPUT PROCEDURE, output file is opened, the sorted record is returned to the Output record area and then the record is written. Finally the file is closed. RETURN workfile RECORD into out-rec.

71. What is the format of a simple MERGE verb? Can INPUT PROCEDURE and OUTPUT PROCEDURE can be specified for MERGE verb?

A. MERGE work file ON ASC/DESC KEY key1…

USING inputfile1, inputfile2…

GIVING outputfile

INPUT PROCEDURE cannot be specified. Only OUTPUT PROCEDURE can be specified

72. How will you position an indexed file at a specific point so that the subsequent sequential operations on the file can start from this point?

A. Use START

START filename KEY IS EQ/GT/LT.. dataname

INVALID KEY …

73. What are the access mode requirements of START statement?

A. Access mode must be SEQUENTIAL or DYNAMIC

74. What are the opening mode requirements of START statement?

  1. Files must be opened in the INPUT or I-O mode.

75.What is the LINKAGE SECTION used for?

A. The linkage section is used to pass data from one program to another program or to pass data from a PROC to a program. It is part of a called program that ‘links’ or maps to data items in the calling program’s working storage. It is the part of the called program where these share items are defined.

76. If you were passing a table via linkage, which is preferable – a subscript or an index?

A. Wake up – you haven’t been paying attention! It’s not possible to pass an index via linkage. The index is not part of the calling programs working storage. Indexing uses binary displacement. Subscripts use the value of the occurrence.

77. What is the difference between a subscript and an index in a table definition?

  1. A subscript is a working storage data definition item, typically a PIC (999) where a value must be moved to the subscript and then increment or decrement it by ADD TO and SUBTRACT FROM statements. An index is a register item that exists outside the program’s working storage. You SET an index to a value and SET it UP BY value and DOWN BY value.

Subscript refers to the array occurrence while index is the displacement (in no of bytes) from the beginning of the array. An index can only be modified using PERFORM, SEARCH & SET. Need to have index for a table in order to use SEARCH, SEARCH ALL Cobol statements.

78. What is an in line PERFORM? When would you use it? Anything else to say about it?

  1. The PERFORM and END-PERFORM statements bracket all COBOL II statements between them. The COBOL equivalent is to PERFORM or PERFORM THRU a paragraph. In line PERFORMs work as long as there are no internal GO TOs, not even to an exit. The in line PERFORM for readability should not exceed a page length – often it will reference other PERFORM paragraphs.

When the body of the Perform will not be used in other paragraphs. If the body of the Perform is a generic type of code (used from various other places in the program), it would be better to put the code in a separate para and use PERFORM paraname rather than in-line perform.

79. What is the use of EVALUATE statement? How do you come out of an EVALUATE statement?

A. Evaluate is like a case statement and can be used to replace nested Ifs. The difference between EVALUATE and case is that no ‘break’ is required for EVALUATE i.e. control comes out of the EVALUATE as soon as one match is made, There is no need of any extra code. EVALUATE can be used in place of the nested IF THEN ELSE statements.

80. What are the different forms of EVALUATE statement?

A. EVALUATE EVALUATE SQLCODE ALSO FILE-STATUS

WHEN A=B AND C=D WHEN 100 ALSO ‘00′

Imperative stmt imperative stmt

WHEN (D+X)/Y = 4 WHEN -305 ALSO ‘32′

imperative stmt imperative stmt

WHEN OTHER WHEN OTHER

imperative stmt imperative stmt

END-EVALUATE END-EVALUATE

 

EVALUATE SQLCODE ALSO A=B EVALUATE SQLCODE ALSO TRUE

WHEN 100 ALSO TRUE WHEN 100 ALSO A=B

imperative stmt imperative stmt

WHEN -305 ALSO FALSE WHEN -305 ALSO (A/C=4)

imperative stmt imperative stmt

END-EVALUATE END-EVALUATE

81. Can you use the INSPECT (with TALLYING option) Cobol verb in a CICS COBOL program?

A. Yes, under COBOL II environment, but not OS/VS COBOL.

82. What is an explicit scope terminator?

  1. A scope terminator brackets its preceding verb, eg. IF .. END-IF, so that all statements between the verb and its scope terminator are grouped together. Other common COBOL II verbs are READ, PERFORM, EVALUATE, SEARCH and STRING.

83. What is the significance of ‘above the line’ and ‘below the line’?

A. Before IBM introduced MVS/XA architecture in the 1980’s a program’s virtual storage was limited to 16 megs. Programs compiled with a 24-bit mode can only address 16 MB of space, as though they were kept under an imaginary storage line. With COBOL II a program compiled with a 31 bit mode can be ‘above the 16 Mb line. (This ‘below the line’, ‘above the line’ imagery confuses most mainframe programmers, who tend to be a literal minded group.)

84. What was removed from COBOL in the COBOL II implementation?

A. Partial list: REMARKS, NOMINAL KEY, PAGE-COUNTER, CURRENT-DAY, TIME-OF-DAY, STATE, FLOW, COUNT, EXAMINE, EXHIBIT, READY TRACE and RESET TRACE.

85. Explain call by context by comparing it to other calls.

A. The parameters passed in a call by context are protected from modification by the called program. In a normal call they are able to be modified.

86. What is the difference between comp and comp-3 usage? Explain other COBOL usages.

  1. Comp is a binary usage, while comp-3 indicates packed decimal. The other common usages are binary and display. Display is the default. Comp is defined as the fastest/preferred numeric data type for the machine it runs on. IBM Mainframes are typically binary and AS400’s are packed.’

87. I understand the possible causes for S0C1 & S0C4 abends, but what are they really?

A. A S0C1 occurs if the CPU attempts to execute binary code that isn’t a valid machine instruction; e.g. if you attempt to execute data. A S0C4 is a memory protection violation. This occurs if a program attempts to access storage beyond the areas assigned to it.

88. What happens when we move a comp-3 field to an edited ( say z(9).zz-)

A. The editing characters are to be used with data items with usage clause as display, which is the default. When you try displaying a data item with usage as computational it does not give the desired display format because the data item is stored as packed decimal. So if u want this particular data item to be edited u have to move it into a data item whose usage is display and then have that particular data item edited in the format desired.

89. What are the causes for S0C1, S0C4, S0C5, S0C7, S0CB abends

  1. S0C1 – May be due to 1.Missing or misspelled DD name 2.Read/Write to unopened dataset 3.Read to dataset opened output 4.Write to dataset opened input 5.Called subprogram not found.

S0C4 may be due to 1.Missing Select statement(during compile) 2.Bad Subscript/index 3.Protection Exception 4.Missing parameters on called subprogram 5.Read/Write to unopened file 6.Move data from/to unopened file.

S0C5 May be due to 1.Bad Subscript/index 2.Close an unopen dataset 3.Bad exit from a perform 4.Access to I/O area(FD) before read.

S0C7 may be due to 1.Numeric operation on non-numeric data 2.Un-initialize working-storage 3.Coding past the maximum allowed sub script. S0CB may be due to 1.Division by Zero

90. What will happen if you code GO BACK instead of STOP RUN in a stand-alone COBOL program i.e. a program which is not calling any other program.

  1. Both give the same results when a program is not calling any other program.

91. What is the difference between an External and a Global Variable ’s?

A. Global variables are accessible only to the batch program whereas external variables can be referenced from any batch program residing in the same system library.

92. WHAT IS REPORT-ITEM?

A. A REPORT-item is a field to be printed that contains EDIT SYMBOLS

93. You are writing report program with 4 levels of totals:city,state,region and country. The codes being used can be the same over the different levels, meaning a city code of 01 can be in any number of states, and the same applies to state and region code show. Do you do your checking for breaks and how do you do add to each level?

  1. Always compare on the highest-level first, because if you have a break at a highest level, each level beneath it must also break. Add to the lowest level for each rec but add to the higher level only on break.

94. What is PSB & ACB?

A. PSB : Program specification block. Inform about how a specific program is to be access one or more IMS DB. It consists of PCB(Prg Communication Block). Information to which segment in DB can be accessed, what the program is allowed to do with those segment and how the DB is to be accessed. ACB : Access Control Blocks are generated by IMS as an expansion of information contained in the PSB in order to speed up the access to the applicable DBD’s.

95. What’s a LDS(Linear Data Set) and what’s it used for ?

A. LDS is a VSAM dataset in name only. It has unstructured 4k (4096 bytes) fixed size CIs which do not contain control fields and therefore from VSAM’s standpoint they do not contain any logical records. There is no freespace, and no access from Cobol. Can be accessed by DB2 and IMS fast path datasets. LDS is essentially a table of data maintained on disk. The ‘table entries’ must be created via a user program and can only be logically accessed via a user program. When passed, the entire LDS must be mapped into storage, then data is accessed via base and displacement type processing.

96. What is the Importance of GLOBAL clause According to new standards of COBOL

A. When any data name, file-name , Record-name, condition name or Index defined in an Including Program can be referenced by a directly or indirectly in an included program, Provided the said name has been declared to be a global name by GLOBAL Format of Global Clause is01 data-1 PIC 9(5) IS GLOBAL.

97. What is the Purpose of POINTER Phrase in STRING command

A. The Purpose of POINTER phrase is to specify the leftmost position within receiving field where the first transferred character will be stored

98.How do we get currentdate from system with century?

A. By using Intrinsic function, FUNCTION CURRENT-DATE

 

99.what is the difference between search and search all in the table handling?

A. Search is a linear search and search all is a binary search.

100.What is the maximum length of a field you can define using COMP-3?

A. 10 Bytes (S9(18) COMP-3).

101.How many Sections are there in Data Division?.

A. SIX SECTIONS 1.’FILE SECTION’ 2.’WORKING-STORAGE SECTION’ 3.’LOCAL-STORAGE SECTION’ 4.’SCREEN SECTION’ 5.’REPORT SECTION’ 6.’LINKAGE SECTION’

In COBOL II, there are only 4 sections. 1.’FILE SECTION’ 2.’WORKING-STORAGE SECTION’ 3.’LOCAL-STORAGE SECTION’ 4.’LINKAGE SECTION’.

102.How can I tell if a module is being called DYNAMICALLY or STATICALLY?

A. The ONLY way is to look at the output of the linkage editor (IEWL)or the load module itself. If the module is being called DYNAMICALLY then it will not exist in the main module, if it is being called STATICALLY then it will be seen in the load module. Calling a working storage variable, containing a program name, does not make a DYNAMIC call. This type of calling is known as IMPLICITE calling as the name of the module is implied by the contents of the working storage variable. Calling a program name literal (CALL).

103.What is the difference between a DYNAMIC and STATIC call in COBOL.

A. To correct an earlier answer:All called modules cannot run standalone if they require program variables passed to them via the LINKAGE section. DYNAMICally called modules are those that are not bound with the calling program at link edit time (IEWL for IBM) and so are loaded from the program library (joblib or steplib) associated with the job. For DYNAMIC calling of a module the DYNAM compiler option must be chosen, else the linkage editor will not generate an executable as it will expect null address resolution of all called modules. A STATICally called module is one that is bound with the calling module at link edit, and therefore becomes part of the executable load module.

104.What is the difference between PIC 9.99 and 9v99?

A. PIC 9.99 is a FOUR-POSITION field that actually contains a decimal point where as PIC 9v99 is THREE-POSITION numeric field with implied or assumed decimal position.

105.How is PIC 9.99 is different from PIC 9v99?

A. PIC 9.99 is a four position field that actually contains a decimal point where as 9v99 is a three position numeric field with an implied or assumed decimal point.

106.what is Pic 9v99 Indicates?

A. PICTURE 9v99 is a three position Numeric field with an implied or assumed decimal point after the first position; the v means an implied decimal point.

107.what guidelines should be followed to write a structured COBOL program?

  1. 1) Use ‘EVALUATE’ stmt for constructing cases. 2) Use scope terminators for nesting. 3)Use in-line Perform stmt for writing ‘do ‘ constructions. 4) Use Test Before and test after in the Perform stmt for writing Do-While constructions.

108.Read the following code.

01 ws-n PIC 9(2) value zero.

a-para.

move 5 to ws-n.

perform b-para ws-n times.

b-para.

move 10 to ws-n.

How many times will b-para be executed ?

A. 5 Times only. it will not take the value 10 that is initialized in the loop.

109.What are some examples of command terminators?

A. END-IF, END-EVALUATE

 

110.What care has to be taken to force program to execute above 16 Meg line?

A. Make sure that link option is AMODE=31 and RMODE=ANY. Compile option should never have SIZE(MAX).BUFSIZE can be 2K, efficient enough.

111.Give some advantages of REDEFINES clause.

A. You can REDEFINE a Variable from one PICTURE class to another PICTURE class by using the same memory location. By REDEFINES we can INITIALISE the variable in WORKING-STORAGE Section itself.3. We can REDEFINE a Single Variable into so many sub-variables.(This facility is very useful in solving Y2000 Problem.)

 

112.Why do we code s9(4)comp. Inspite of knowing comp-3 will occupy less space.

A. Here s9(4)comp is small integer ,so two words equal to 8 bytes. Totally it will occupy 2 bytes(4 words).here in s9(4) comp-3 as one word is equal to 1/2 byte.4 words equal to 2 bytes and sign will occupy 1/2 bytes totally it will occupy 3 bytes.

 

113.The maximum number of dimensions that an array can have in COBOL-85 is ________.

Answer: SEVEN in COBOL – 85 and THREE in COBOL – 84

114.Name the divisions in a COBOL program.

A. IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION, PROCEDURE DIVISION.

115.What are the different data types available in COBOL?

A. Alpha-numeric (X), alphabetic (A) and numeric (9).

116.What is 77 level used for ?

A. Elementary level item. Cannot be subdivisions of other items (cannot be qualified), nor can they be subdivided themselves.

117.What is 88 level used for ?

A. For defining condition names.

118.What is level 66 used for ?

A. For RENAMES clause.

119.What does the IS NUMERIC clause establish?

A. IS NUMERIC can be used on alphanumeric items, signed numeric & packed decimal items and unsigned numeric & packed decimal items. IS NUMERIC returns TRUE if the item only consists of 0-9. However, if the item being tested is a signed item, then it may contain 0-9, + and – .

120.My program has an array defined to have 10 items. Due to a bug, I find that even if the program access the 11th item in this array, the program does not abend. What is wrong with it?

A. Must use compiler option SSRANGE if you want array bounds checking. Default is NOSSRANGE.

121. What is the difference between performing a SECTION and a PARAGRAPH?

A. Performing a SECTION will cause all the paragraphs that are part of the section, to be performed.

Performing a PARAGRAPH will cause only that paragraph to be performed.

122.Can I redefine an X(200) field with a field of X(100) ?

  1. Yes.

123.What does EXIT do?

A. Does nothing ! If used, must be the only sentence within a paragraph.

124.Can I redefine an X(100) field with a field of X(200)?

A. Yes. Redefines just causes both fields to start at the same location. For example:

01 WS-TOP PIC X(1)

01 WS-TOP-RED REDEFINES WS-TOP PIC X(2).

If you MOVE ‘12′ to WS-TOP-RED,

DISPLAY WS-TOP will show 1 while

DISPLAY WS-TOP-RED will show 12.

125.Can I redefine an X(200) field with a field of X(100) ?

A. Yes.

126.What do you do to resolve SOC-7 error?

A. Basically you need to correcting the offending data. Many times the reason for SOC7 is an un-initialized numeric item. Examine that possibility first. Many installations provide you a dump for run time abends ( it can be generated also by calling some subroutines or OS services thru assembly language). These dumps provide the offset of the last instruction at which the abend occurred. Examine the compilation output XREF listing to get the verb and the line number of the source code at this offset. Then you can look at the source code to find the bug. To get capture the runtime dumps, you will have to define some datasets (SYSABOUT etc ) in the JCL. If none of these are helpful, use judgement and DISPLAY to localize the source of error. You may even use batch program debugging tools.

127.How is sign stored in Packed Decimal fields and Zoned Decimal fields?

A. Packed Decimal fields: Sign is stored as a hex value in the last nibble (4 bits ) of the storage. Zoned Decimal fields: As a default, sign is over punched with the numeric value stored in the last bite.

128.How is sign stored in a comp-3 field?

A. It is stored in the last nibble. For example if your number is +100, it stores hex 0C in the last byte, hex 1C if your number is 101, hex 1D if the number is -101, hex 2D if the number is -102 etc…

129.How is sign stored in a COMP field ?

A. In the most significant bit. Bit is on if -ve, off if +ve.

130.What is the difference between COMP & COMP-3 ?

A. COMP is a binary storage format while COMP-3 is packed decimal format.

131.What is COMP-1? COMP-2?

  1. COMP-1 – Single precision floating point. Uses 4 bytes.

COMP-2 – Double precision floating point. Uses 8 bytes.

132.How do you define a variable of COMP-1? COMP-2?

A. No picture clause to be given. Example 01 WS-VAR USAGE COMP-1.

133.How many bytes does a S9(7) COMP-3 field occupy ?

A. Will take 4 bytes. Sign is stored as hex value in the last nibble.

General formula is INT((n/2) + 1)), where n=7 in this example.

134.How many bytes does a S9(7) SIGN TRAILING SEPARATE field occupy ?

A. Will occupy 8 bytes (one extra byte for sign).

135.What is the maximum size of a 01 level item in COBOL I? in COBOL II?

  1. In COBOL II: 16777215

136.What is COMP SYNC?

A. Causes the item to be aligned on natural boundaries. Can be SYNCHRONIZED LEFT or RIGHT.

For binary data items, the address resolution is faster if they are located at word boundaries in the memory. For example, on main frame the memory word size is 4 bytes. This means that each word will start from an address divisible by 4. If my first variable is x(3) and next one is s9(4) comp, then if you do not specify the SYNC clause, S9(4) COMP will start from byte 3 ( assuming that it starts from 0 ). If you specify SYNC, then the binary data item will start from address 4. You might see some wastage of memory, but the access to this comp field is faster.

137.How do you reference the following file formats from COBOL programs?

  1. Fixed Block File – Use ORGANISATION IS SEQUENTIAL. Use RECORDING MODE IS F, BLOCK CONTAINS 0.
  2. Fixed Unblocked – Use ORGANISATION IS SEQUENTIAL. Use RECORDING MODE IS F, do not use BLOCK CONTAINS.
  3. Variable Block File – Use ORGANISATION IS SEQUENTIAL. Use RECORDING MODE IS V, BLOCK CONTAINS 0. Do not code the 4 bytes for record length in FD. i.e. JCL record length will be max record length in program + 4
  4. Variable Unblocked – Use ORGANISATION IS SEQUENTIAL. Use RECORDING MODE IS V, do not use BLOCK CONTAINS. Do not code 4 bytes for record length in FD ie JCL rec length will be max rec length in pgm + 4.
  5. ESDS VSAM file – Use ORGANISATION IS SEQUENTIAL.
  6. KSDS VSAM file – Use ORGANISATION IS INDEXED, RECORD KEY IS, Alternate Record Key Is
  7. RRDS File – Use ORGANISATION IS RELATIVE, RELATIVE KEY IS
  8. Printer File – Use ORGANISATION IS SEQUENTIAL. Use RECORDING MODE IS F, BLOCK CONTAINS 0. (Use RECFM=FBA in JCL DCB).

138.What are different file OPEN modes available in COBOL? In which modes are the files Opened to write.

A. Different Open modes for files are INPUT, OUTPUT, I-O and EXTEND. Of which Output and Extend modes are used to write new records into a file.

139.In the JCL, how do you define the files referred to in a subroutine?

A. Supply the DD cards just as you would for files referred to in the main program.

140.Can you REWRITE a record in an ESDS file? Can you DELETE a record from it?

A. Can rewrite(record length must be same), but not delete.

141.What is file status 92?

A. Logic error. e.g., a file is opened for input and an attempt is made to write to it.

142.What is file status 39?

A. Mismatch in LRECL or BLOCKSIZE or RECFM between your COBOL pgm & the JCL (or the dataset label). You will get file status 39 on an OPEN.

143.What is Static, Dynamic linking ?

A. In static linking, the called subroutine is link-edited into the calling program , while in dynamic linking, the subroutine & the main program will exist as separate load modules. You choose static/dynamic linking by choosing either the DYNAM or NODYNAM link edit option. (Even if you choose NODYNAM, a CALL identifier (as opposed to a CALL literal), will translate to a DYNAMIC call).

A statically called subroutine will not be in its initial state the next time it is called unless you explicitly use INITIAL or you do a CANCEL. A dynamically called routine will always be in its initial state.

144.Explain NEXT and CONTINUE verbs for file handling.

A The Continue verb is used for a situation where there in no EOF condition. i.e. The records are to be accessed again and again in a file. Whereas in the next verb the indexed file is accessed sequentially, whence when index clause is accessed sequentially read next record command is used.

145.What is AMODE(24), AMODE(31), RMODE(24) and RMODE(ANY)? (applicable to only MVS/ESA).

A. These are compile/link edit options. AMODE – Addressing mode. RMODE – Residency mode.

AMODE(24) – 24 bit addressing. AMODE(31) – 31 bit addressing. AMODE(ANY) – Either 24 bit or 31 bit addressing depending on RMODE. RMODE(24) – Resides in virtual storage below 16 Meg line. Use this for 31 bit programs that call 24 bit programs. (OS/VS Cobol pgms use 24 bit addresses only). RMODE(ANY) – Can reside above or below 16 Meg line.

146.What compiler option would you use for dynamic linking?

  1. DYNAM.

147.What is SSRANGE, NOSSRANGE ?

A. These are compiler options w.r.t subscript out of range checking. NOSSRANGE is the default and if chosen, no run time error will be flagged if your index or subscript goes out of the permissible range.

148.How do you set a return code to the JCL from a COBOL program?

A. Move a value to RETURN-CODE register. RETURN-CODE should not be declared in your program.

149.How can you submit a job from COBOL programs?

A. Write JCL cards to a dataset with

//xxxxxxx SYSOUT=(A,INTRDR) where ‘A’ is output class, and dataset should be opened for output in the program. Define a 80 byte record layout for the file.

150.What are the differences between OS VS COBOL and VS COBOL II?

  • OS/VS Cobol pgms can only run in 24 bit addressing mode, VS Cobol II pgms can run either in 24 bit or 31 bit addressing modes allowing program to address above 16 Meg main storage line.
  • Report writer is supported only in OS/VS Cobol.
  • USAGE IS POINTER is supported only in VS COBOL II.
  • Reference modification eg: WS-VAR(1:2) is supported only in VS COBOL II.
  • COBOL II introduces new features (EVALUATE, SET … TO TRUE, CALL … BY CONTEXT, etc)
  • Scope terminators are supported in COBOL II.
  • OS/VS Cobol follows ANSI 74 stds while VS COBOL II follows ANSI 85 stds.
  • Under CICS Calls between VS COBOL II programs are supported.
  • COBOL II supports structured programming by using in-line PERFORM ’s.
  • COBOL II does not support old features (READY TRACE, REPORT-WRITER, ISAM, etc.).
  • In non-CICS environment, it is possible. In CICS, this is not possible.

NTLM Authentication

Tuesday, March 24th, 2009

Introduction:

This BOK details how to get a Java-based web-application to negotiate with a IE web client for username and domain information. This is a common requirement for web-based applications especially ones that do not want to bore users with a login page. IE will negotiate a user’s password hashes with the webserver, which checks their authenticity against a windows domain controller. If valid, the user’s username and domain will be accessible to the webserver servlets.

NTLM Authentication and how we achieve it:

 
The method HttpServletRequest.getRemoteUser() should return the username of the person using the browser which fired a request to this Servlet.
This method, however works correctly only if the user has been authenticated first by a webserver authentication scheme -
which could be BASIC,DIGEST or CLIENT-CERT. This is the kind of setup the the Apache webserver provides, giving a challenge-response, username-password method of authentication.

What we do here is use a Servlet filter provided as part of the open-source jCIFS package, to get an IE user's username and domain. 

This filter will take the trouble of intercepting user requests, asking IE for the user's password hashes,validating them against a windows domain controller and enabling HttpServletRequest.getRemoteUser() to return the windows user id.
 
Please note this method will not work for non-IE clients, simply because this is a proprietary extension by Microsoft. 
 
For other browsers you will have to rely on BASIC or certificate-based authentication.

How to setup your web application:

 First, we need to download a jcifs jar from http://jcifs.samba.org. I have tested this with jcifs version 0.7.14.jCIFS is from the makers of Samba and provides APIs to access Windows shares, networks and the ability to authenticate against a Windows domain controller. Place this jar under WEB-INF/lib of your web application. There is a filter called jcifs.http.NtlmHttpFilter which implements all the wizadry above. You need to register it in your application's web.xml descriptor:
 
<web-app>
...
    <!-- NTLM HTTP Authentication only works with MSIE -->
    
    <filter>
        <filter-name>NTLM HTTP Authentication Filter</filter-name>
        <filter-class>jcifs.http.NtlmHttpFilter</filter-class>

        <!-- CCD will help you with a PDC and WINS server ip at your location. -->
        <init-param>
            <param-name>jcifs.http.domainController</param-name>
            <param-value>192.168.170.5</param-value>
        </init-param>

        <init-param>
            <param-name>jcifs.netbios.wins</param-name>
            <param-value>192.168.166.13</param-value>
        </init-param>
    </filter>

    <!-- This is the url under which we need access to the username and domain. -->

    <filter-mapping>
        <filter-name>NTLM HTTP Authentication Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

...

</web-app>
 
That's it. Now all IE requests to your webserver urls as specified in the web.xml entries are negotiated so that you can call a HttpServletRequest.getRemoteUser() to get the remote user's username in the form.

            DOMAIN\username.

Please note at no point will a password dialog pop up for the user, the password hashes are picked from IE and validated with the domain controller.
 
 
Example code for a servlet :
 
 
public void doGet( HttpServletRequest req,
                        HttpServletResponse resp )
throws IOException, ServletException
        
        {

PrintWriter out = resp.getWriter();

resp.setContentType( “text/html” );
out.println( “<HTML><HEAD><TITLE>NTLM HTTP Authentication Example</TITLE></HEAD><BODY>” );
out.println( “<h2>NTLM HTTP Authentication Example</h2>” );

out.println( req.getRemoteUser() + ” logged in” );

 

}

If the filter has not been configured properly, a null will be printed for the above call to req.getRemoteUser().

 

References:

  • Web Link :- http://jcifs.samba.org.

Servlet for Beginners

Tuesday, March 24th, 2009

Servlet for Beginners

 

 

 

 

Introduction

 

Web applications are generally based upon Client Server Architecture in which a client sends a request to the server. Depending upon the request, the server responds to the client providing it the required information. To handle such client requests, server side programs are needed. Here comes the need of servlet .A servlet can be defined as a server side program which offers all the advantages of java which includes platform independency also.

 

Example of client request:

 

One of the most common real life scenarios using client server architecture is: web based mail server. When a user enters his userId and password, he is requesting the server to provide him with his mailbox and all other data. Here user doesn’t invoke any program to do all such task but a server side program or set of server side programs do the entire related task. The client request is mapped to a servlet.

 

A user can never invoke the servlet .Servlets are server invoked programs.

 

Java Servlet API 2.3 is defined in java packages: javax.servlet, javax.servlet.http

The package javax.servlet is generic to all protocols i.e. it is protocol independent while javax.servlet.http is specific to http protocol.

 

Some Basic Terminology to have a better understanding of Servlets:

 

Web Container:  A web container provides run time environment for working of web components. The main task of a web container is life cycle management of all the web components. Also it supports creation of objects for the arguments, security related tasks and concurrency control.

 

 

Life Cycle Of A Servlet

 

Controlling the life cycle of a servlet is the task of the container in which servlet is deployed. The life cycle of a servlet has following phases:

 

Initializing a servlet or Birth of a servlet:

 

When a request is first time mapped to a servlet, the container loads the servlet. Then instance of the servlet is created. Then, to instantiate that instance init () is invoked. The Servlet interface provides declaration of init ().To customize this instantiation process, init () method needs to be overridden. If the instantiation can not be completed successfully, UnavailableException should be thrown by such servlet. This method is invoked by any servlet only once in a life time i.e. when it is being loaded first time. The initialization variables, if any, should be placed in this method.

 

Method signature: public void init(ServletConfig config) throws ServletException

 

Providing service for a servlet:

 

After a servlet instance has been instantiated, it should be able to do the needed task i.e. to handle the client side requests. When a servlet is initialized in an appropriate manner, it means an instance of servlet is available. Now each time the server receives request, a new thread is spawned from the same instance and the service () method is invoked. All the application logic is included in this method. It acts as an entry point. The service () method, by default checks the HTTP header of the request to check whether it needs to call doGet () method or doPost () method .In case, both methods need to be invoked, then any of the methods i.e. doPost () and doGet() can call another one.

 

 

Method signature: public void service (ServletRequest request, ServletResponse

                                                     response) throws ServletException, IOException    

                                                                       

 

Destroying/Finalizing a servlet or Death of servlet:

 

A servlet should be destroyed when its task is over. The container invokes destroy () method for a servlet instance when it determines that the services of this servlet are not needed now. Invocation of destroy () method releases all the memory resources being used by the servlet.A servlet should be removed from memory only after all its service methods have been completed. It is the place for all clean up tasks.

 

Method signature: public void destroy ()

 

Discussing the Basic Interfaces and classes in servlet API:

 

1 Servlet interface

2 GenericServlet class

3 HttpServlet class

4 ServletRequest interface

5 ServletResponse interface

6 HttpServletRequset interface

7 HttpServletResponse interface

 

1.  Servlet interface:  This is the interface which a class should implement to become

     a servlet.The following methods must be implemented while implementing

     javax.servlet.Servlet interface:

 

 

è       public void init(ServletConfig config)

è    public void service(ServletRequest request, ServletResponse response)

è    public void destroy()

è    public ServletConfig getServletConfig()

è    public String getServletInfo()

 

 

2.   GenericServlet class: This is the class to provide basic implementation for Servlet

       interface.

 

      public abstract class GenericServlet implements Servlet, ServletConfig,

      Serializable

       

 

       

       It provides following additional methods:

 

 

è       public init()

è       public void log(String message)

è       public void log(String message, Throwable t)

 

 

3.     HttpServlet class: This is a subclass of GenericServlet class.It provides HTTP

      specific implementation of Servlet interface. Mostly the servlet classes

      extend this class to provide functionality of a servlet.

 

            public abstract class HttpServlet extends GenericServlet implements

            Serializable

 

            Here are the mainly used methods as specified in this class:

 

è    service() methods :

 

      There are two variants of service () methods in HttpServlet class as

      discussed below:

 

public void service (ServletRequest request, ServletResponse response) throws ServletException, IOException

 

 

protected void service (HttpServletRequest request, HttpResponse response) throws ServletException,IOException

 

 

The public service method is implementation of GenericServlet class. Practically the above service methods should not be overridden if only default behavior is needed. In case when additional behavior is required, these methods need to be overridden including the default behavior using super.service().

 

When the container receives a new request, it invokes the methods in following sequence:

 

a)    Firstly, the public service () method is invoked.

 

b)    The arguments are casted to HttpServletRequest and HttpServletResponse respectively and then the public service () method invokes a call to protected service () method.

 

c)    The protected service() method acts as a delegation method to delegate requests to doXXX() methods depending upon the type of HTTP request.

 

è  doXXX() methods:

 

The following protected methods are implemented by HttpServlet class in order to serve different types of client requests:

 

*       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

 

 

*       protected void doPost(HttpServletRequest request, HttpServletResponse   

response)  throws ServletException,IOException

 

*       protected void doHead(HttpServletRequest request, HttpServletResponse

response) throws ServletException,IOException

 

*       protected void doDelete(HttpServletRequest request, HttpServletResponse

response) throws ServletException,IOException

 

 

*       protected void doOptions(HttpServletRequest request, HttpServletResponse

                                                response) throws ServletException,IOException

 

*       protected void doPut(HttpServletRequest request, HttpServletResponse   

                                     response) throws ServletException, IOException

 

*       protected void doTrace(HttpServletRequest request, HttpServletResponse

response)throws ServletException,IOException

 

 

 

4.     ServletRequest interface:

 

 When the service () method of GenericServlet or the HttpServlet is called, an 

  instance of this object is created by the container.

 

  public interface ServletRequest.

 

  Some of the methods provided by this interface are discussed as below. These

  methods are used to access request parameters from the clients and to         

        manage the request attributes:

 

*       public String getParameter(String name): returns value of the parameter

      corresponding to the key ‘name’. It returns the first value of the list if  

      multiple entries are found corresponding to the same key. And a null value is

      returned if such parameter is not found.

 

*       public String[] getParameterValues(String name) : returns complete list of

      values i.e. returns a String array corresponding to the key ‘name’.

 

*       public Enumeration getParameterNames(): returns name of all parameters in

      the form of Enumeration. An empty enumeration is returned if no request

      parameters are associated with the request being processed.

 

*       public Map getParameterMap() : returns a Map object containing name of

      parameters as keys and set of their corresponding value being the values.

 

*       public Object getAtribute (String name): returns the value of the attribute named as ‘name’. A null is returned if no such attribute exists.

 

*       public Enumeration getAttributeNames(): returns Enumeration object      

      containing names of all attributes.

 

*       public void setAttribute(String name, Object attribute): sets value of attribute

      for a named attribute.

 

*       public void removeAttribute(String name): The named attribute is removed

      from the request. The setting and removal of attributes is useful to maintain

      sessions.

 

5.     ServletResponse interface:

 

  When the service () method of GenericServlet or the HttpServlet is called, an 

  instance of this object is created by the container.

 

   public interface ServletResponse

               

        Some of the important methods provided by this interface are discussed as   

          below:

 

*       public void setContentType(String type) : This method is used to set the

            MIME type for the response. A PrintWriter object is requested to write body 

            data to the response. Before such a request can be made, all response

            headers must be set.

 

*       public java.io.PrintWriter getWriter() throws java.io.Exception: For the same

            HttpServletResponse object, this method should be called only once. This   

            method is used to send character text in response.

 

*       public void resetBuffer(): This is used to reset the underlying buffer i.e. it

            cleans up the underlying buffer by deleting the contents .The status code or

            the response header remains unaffected after invocation of this method.

 

6.        HttpServletRequest  interface :

 

          This interface implements the ServletRequest interface. Hence, all the

          methods of ServletRequest are well available here. It is specific to HTTP

          requests.

 

          Mainly used methods in this interface are the methods which are used to

          access request parameters.

 

    Some other commonly used methods are:

   

 

a)    public String getQueryString() :

b)    public String getHeader(String name)

c)    public String getMethod() : returns type of HTTP request

 

7.        HttpServletResponse interface:

 

         This interface implements the ServletResponse interface. Hence, all the  

         methods of ServletResponse are well available here. It is specific to HTTP

         response.

    

   Some of the methods :

 

a)    public void sendError (int status): This method is used to output various status codes supported by HTTP.

 

b)    public String encodeURL(String url)

 

                             

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

A Simple Servlet

 

/* To import some packages which contain classes which are used by   

     servlet */

 

import javax.servlet.*;

import javax.servlet.http.*;

 

 

/* The servlet ‘SimpleServlet’ extends javax.servlet.http.HttpServlet which              

     is the base class for HTTP servlets */

 

public class SimpleServlet extends HttpServlet

{

 

     /* Overriding doGet() method of HTTPservletRequest */

 

         protected void doGet (HttpServletRequest request, HttpServletResponse

                                        response) throws ServletException, IOException

 

         {

                              /* Setting the content type for output using method of

                                   HttpServletResponse object */

 

                                     response.setContentType(“text/html”);

 

                             /* A PrintWriter object is being requested to write a

                                  response */

 

                                     PrintWriter out= response.getWriter();

 

                             /* Using the PrintWriter object which was obtained in  

                                   Previous step to write response of type text/html */

 

                                     out.println(“<HTML><HEAD><TITLE> A Simple “+

                                                   “Servlet</TITLE></HEAD></BODY></HTML>”);

 

                            /* Closing the PrintWriter object after the response has         

                                 been written */

 

                                     out.close();

         }

}

 

 

 

 

 

 

 

 

 

 

Single Thread Model in Servlets

 

For each servlet name there exists one servlet instance. For all other incoming requests, new threads are spawned from the same instance. That is, service () method might be invoked concurrently by many threads. What if this concurrent execution of service () method is to be avoided?

 

Java Servlet API defines a marker interface named as javax.servlet.SingleThreadModel to provide alternative if the service () method is thread unsafe. This interface is just a marker interface i.e. it does not provide definition of any method but its function is to let the container know that only one thread should be able to execute the service() method at any time.

 

It is suggested to avoid SingleThreadModel .This interface SingleThreadModel is deprecated in servlet API 2.4.

 

There are two approaches that a container may follow in order to ensure that no two threads can concurrently access the service () method. Also combination of both approaches can also be used:

 

v  Request Serialization:

 

              A single instance of the servlet is used for all client requests. Instead of sending multiple requests to that instance at the same time, the requests are sent to the instance in a serialized manner. While one client is being served, all the other

requests will keep waiting for availability of instance.

 

 

v  Instance Pooling:

 

              A pool of servlet instances is maintained by the container. The container picks up an instance from the pool for each new incoming request. The instance is  

returned to the pool after the request has been served.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Distinguishing among different users via concept of session:

 

A session is basically a unique connection between a server and a client to identify that multiple requests are sent by same client i.e. a session enables the server to identify a client across multiple requests.

 

Example:

 

After being login to an email account, the client needs to be identified by the server i.e. The server needs to verify that all the tasks like writing new mail, sending new mail are being done by the same authenticated client.However, the client may be asked to authenticate himself again and again but it is not a practical approach for large business transactions.

 

Other example is of banking transactions.

 

In case of large business transactions, flexibility can be maintained using facilities like:

 

Session:

 

It is required by the server to distinguish among different users. This can be achieved by putting specific request in a specific working session

 

State:

 

State refers to the information associated with the requests. Sometimes the information related to the previous requests is also required to handle a new incoming request.

 

The protocols which maintain the state are known as stateless protocols while stateless is the term used for protocols in which server do not remember any information about previous requests and treat each new request, even from the same client ,as a fresh request. HTTP is a stateless protocol.

 

 

 Maintaining the servlet sessions using Java Servlet API:

 

Java Servlet API provides an interface HttpSession which is used to represent session objects. And HttpServletRequest interface provides methods to create and track session:

 

a)    public HttpSession getSession( boolean create):

 

This method returns the HttpSession object which is associated with the request object representing the request being currently processed. The boolean argument tells the container whether new session is to be created if session does not exist already. If value of ‘create’ is true then a new session is created in case a session does not exist already. However, a null is returned if a session does not exist and  value of ‘create’ is false.

 

 

b)    public HttpSession getSession() :

 

It is similar to the above method with the only difference that it always create a session if one does not exist already.

 

A session consumes memory and other resources. So a session should be closed when not required. For ex: if a user closes his mail application without being logging out, the session is still using resources i.e. session is in inactive state. So it should be closed.

 

The methods provided by HttpSession interface can be grouped as:

 

è  Methods which deal with session lifetime

 

a)    public long getCreationTime() :

 

       This method returns the time of creation of session since January 1, 1970  

        00:00:00 GMT

 

b)    public int getMaxInactiveInterval():

 

        This method returns the time duration in seconds for which session will

        remain active between requests before expiring.

 

c)    public int setMaxInactiveInterval(int interval):

 

It is used to set the time duration in seconds for which session will remain active between requests before expiring .The session is automatically terminated after the specified time interval.

 

d)    public Boolean isNew() :

 

      It returns true or false indicating whether the session is new or not.

 

e)    public void invalidate() :

 

       This method is used to terminate a session. When a user logs out, this

       method is invoked to terminate his session.

 

 

 

è  Methods which associate state with sessions

 

 

a) public Object getAttribute(String name)

b) public Enumeration getAttributeNames(String name)

c) public setAttribute(String name, Object value)

d) public void removeAttribute(String name)

 

 (These methods have been discussed under ServletRequest interface)

 

 

 

 

References :

 

 

 http://www.iam.ubc.ca/guides/javatut99/servlets/lifecycle/index.html

 

 

       http://www.ecerami.com/applied_spring_2004/lectures/life_cycle.ppt

 

 

       http://java.sun.com/j2ee/tutorial/1_3-fcs/index.html

 

 

        http://www.stardeveloper.com/articles/display.html?article=2001062001&page=1

 

 

        

 

 

 

 

Servlets FAQ

Tuesday, March 10th, 2009

TABLE OF CONTENTS

How does the performance of JSP pages compare with that of servlets? How does it compare with Perl scripts?

What’s a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization?

How can I further optimize my servlets?

How do I set my CLASSPATH for servlets?

Why doesn’t my servlet work inside a <SERVLET> tag?

How do I fully shut down the server? My browser says “the server returned an invalid or unrecognized response” — what gives?

What is the HelloWorld Servlet?

How do I get the name of the currently executing script?

How do I mix JSP and SSI #include?

How do I ensure that my servlet is thread-safe?

How do I use Session Tracking?

How can I detect whether the user accepted my cookie?

How do I integrate HTML design into my Servlet? How do I send email from a servlet?

Are there any ISPs that will host my servlets?

What is the difference between URL encoding and URL rewriting?

How can my applet communicate with my servlet?

How can I debug my servlet? How do I create an image (GIF, JPEG, etc.) on the fly from a servlet?

How do I upload a file to my servlet?

How do I access a database from my servlet?

 

 

 

 

How does the performance of JSP pages compare with that of servlets? How does it compare with Perl scripts?
 

The performance of JSP pages is very close to that of servlets. However, users may experience a perceptible delay when a JSP page is accessed for the very first time. This is because the JSP page undergoes a “translation phase” wherein it is converted into a servlet by the JSP engine. Once this servlet is dynamically compiled and loaded into memory, it follows the servlet life cycle for request processing. Here, the jspInit() method is automatically invoked by the JSP engine upon loading the servlet, followed by the _jspService() method, which is responsible for request processing and replying to the client. Do note that the lifetime of this servlet is non-deterministic – it may be removed from memory at any time by the JSP engine for resource-related reasons. When this happens, the JSP engine automatically invokes the jspDestroy() method allowing the servlet to free any previously allocated resources.

Subsequent client requests to the JSP page does not result in a repeat of the translation phase as long as the servlet is cached in memory, and are directly handled by the servlet’s service() method in a concurrent fashion (i.e. the service() method handles each client request within a seperate thread concurrently.)

There have been some recent studies contrasting the performance of servlets with Perl scripts running in a “real-life” environment. The results are favorable to servlets, especially when they are running in a clustered environment. For details, see:

http://www.objexcel.com/workingjava.htm#Web Server Benchmarks

 

What’s the difference between the JSDK and the JSWDK? And what’s the current version?
 

The kit for developing servlets, containing the Servlet API classes and tools, used to be called the Java Servlet Development Kit (JSDK). Then Sun renamed the Java Development Kit (JDK) to the Java 2 Software Development Kit (J2SDK). Since J2SDK sounds a lot like JSDK, the Servlet team renamed JSDK to JavaServer Web Development Kit (JSWDK). (They also added support for JSPs.)

Here’s where it gets confusing. When they renamed it, they also renumbered it. So the JSWDK 1.0 is actually more recent than the JSDK 2.1. It’s also confusing that when people want to develop servlets, they have to download something called a JavaServer Web Development Kit, which sounds like it should be used to develop servers, not servlets.

A further confusion is that the Servlet spec is developed independently from the JSP spec, and they both have different versions than the JSDK/JSWDK.

The following table summarizes the confusion.

 

Product Version Servlet spec JSP spec
Tomcat 3.0 2.2 1.1
JSWDK 1.0.1 2.1 1.0.1
JSDK 2.1 2.1 none
JSDK 2.0 2.0 none

Make sure you look for the “W”!

 

Can a servlet maintain a JTA UserTransaction object across multiple servlet invocations?
 

No. A JTA transaction must start and finish within a single invocation (of the service() method). Note that this question does not address servlets that maintain and manipulate JDBC connections, including a connection’s transaction handling.

 

What’s a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization?
 

Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. If you anticipate your users to increase in the future, you may be better off implementing explicit synchronization for your shared data. The key however, is to effectively minimize the amount of code that is synchronzied so that you take maximum advantage of multithreading.

Also, note that SingleThreadModel is pretty resource intensive from the server’s perspective. The most serious issue however is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the unserviced requests are queued until something becomes free – which results in poor performance. Since the usuage is non-deterministic, it may not help much even if you did add more memory and increased the size of the instance pool.

 

How can I further optimize my servlets?
 

You can always wring out some efficiency by making use of a StringBuffer or ByteArray.

For example, instead of sending your HTML to the client using a PrintWriter or some other output stream, you can write it out to a StringBuffer, and send it to the client using just one write invocation. Of course, you’ll have to indicate the length of your data stream in the HTTP header too as shown below:

 PrintWriter out = res.getWriter();

 StringBuffer sb = new StringBuffer();

 …

 //concatenate html to StringBuffer

 //set len and write it out

 res.setContentLength(sb.length());

 out.print(sb);

 

How do I set my CLASSPATH for servlets?
 

That depends.

For developing servlets, just make sure that jsdk.jar (in the lib subdirectory of the JSDK (http://java.sun.com/products/servlet/) ) is in your CLASSPATH, and use your normal development tools (javac and so forth).

For running servlets, you need to set the CLASSPATH for your servlet engine. This varies from engine to engine. Each has different rules for how to set the CLASSPATH, which libraries and directories should be included, and which libraries and directories should be excluded. Note: for engines that do dynamic loading of servlets (e.g. JRun, Apache Jserv), the directory containing your servlet class files shoud not be in your CLASSPATH, but should be set in a config file. Otherwise, the servlets may run, but they won’t get dynamically reloaded.

The Complete CLASSPATH Guide for Servlets (http://www.meangene.com/java/classpath.html) by Gene McKenna (mckenna@meangene.com) has detailed instructions on how to set your CLASSPATH for JavaWebServer and JRun.

 

Is it the “servlets” directory or the “servlet” directory?
 

For Java Web Server:

  • on the file system, it’s “servlets”
    c:\JavaWebServer1.1\servlets\DateServlet.class
  • in a URL path, it’s “servlet”
    http://www.stinky.com/servlet/DateServlet

Other servlet engines have their own conventions. Usually on the file system it’s “servlets” and in a URL it’s “/servlet” (which is an alias or virtual path). ]

 

Why doesn’t my servlet work inside a <SERVLET> tag?
 

If you use your servlet inside an SSI, you must use res.getOutputStream() and not res.getWriter(). Check the server error logs for more details.

 

How do I support both GET and POST protocol from the same Servlet?
 

The easy way is, just support POST, then have your doGet method call your doPost method:

 

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException

{

    doPost(req, res);  

}

Note that implementing the service() method is usually not what you want to do, since HttpServlet provides its own implementation of service() that turns around and calls doGet(), doPost(), etc.

Lee Crocker (LCrocker@INFORMANT.COM): “It’s probably cleaner not to override service() when extending HttpServlet. The existing service method just calls doGet(), doPost(), etc. as appropriate, so you can certainly override it if you feel like it, but then you wind up not only treating GET and POST identically, but also all other HTTP commands, like HEAD, TRACE, and OPTIONS. If you want GET and POST to do the same thing, just have doGet() and doPost() call the same private method that does all the work.”

 

How do I fully shut down the server?
 

For JWS, under Windows, pressing control-C doesn’t fully shut down the server. You should use the Admin Tool and click “Shut Down”. (Or you can hit ctl-alt-del, find “JREW” in the list, and “End Task”.)

 

My browser says “the server returned an invalid or unrecognized response” — what gives?

 

This is probably due to a NullPointerException being thrown. There’s a bug in JWS 1.1 whereby it doesn’t correctly log these exceptions.

The solution is to put your doPost() method inside a try block and catch NullPointerException. See the debugging question in this FAQ for more details and source code.

 

What is the HelloWorld Servlet?

 

public class HelloHttpServlet extends HttpServlet

{

    public void doGet(HttpServletRequest req, HttpServletResponse res)

    throws IOException, ServletException

    {

          String name = req.getParameter(”name”);

          if (name == null) name = “Joe”;

          res.setContentType(”text/plain”);

          ServletOutputStream out = res.getOutputStream();

          out.println(”Hello, ” + name + “!”);

    }

}

This code responds to an invocation of the form

 

http://myserver.foo.com/servlet/HelloHttpServlet?name=Fred

 

How do I get the name of the currently executing script?
 

Use req.getRequestURI() or req.getServletPath(). The former returns the path to the script including any extra path information following the name of the servlet; the latter strips the extra path info. For example:

URL http://www.purpletech.com/servlets/HelloEcho/extra/info?height=100&width=200
getRequestURI /servlets/HelloEcho/extra/info
getServletPath /servlets/HelloEcho
getPathInfo /extra/info
getQueryString height=100&width=200

This is useful if your form is self-referential; that is, it generates a form which calls itself again. For example:

 

out.println(”<FORM METHOD=POST ACTION=\”" +

        res.encodeURL(req.getServletPath()) +

        “\”>”);

out.println(”<INPUT NAME=value>”);

out.println(”<INPUT TYPE=submit>”);

out.println(”</FORM>”);

(encodeURL adds session information if necessary. See the Sun Servlet Tutorial and this FAQ’s Session Tracking question. Note that this method was renamed from “encodeUrl” to the properly-capitalized “encodeURL” somewhere around version 2.1 of the servlet spec.)

Note that early versions of Java Web Server and some servlet engines had a bug whereby getRequestURI would also return the GET parameters following the extra path info.

 

How do I mix JSP and SSI #include?

 

If you’re just including raw HTML, use the #include directive as usual inside your .jsp file.

 

<!–#include file=”data.inc”–>

But it’s a little trickier if you want the server to evaluate any JSP code that’s inside the included file. Ronel Sumibcay (ronel@LIVESOFTWARE.COM) says: If your data.inc file contains jsp code you will have to use

 

<%@ vinclude=”data.inc” %>

The <!–#include file=”data.inc”–> is used for including non-JSP files.

 

How do I ensure that my servlet is thread-safe?

 

This is actually a very complex issue. A few guidelines:

  • 1. The init() method is guaranteed to be called once per servlet instance, when the servlet is loaded. You don’t have to worry about thread safety inside this method, since it is only called by a single thread, and the web server will wait until that thread exits before sending any more threads into your service() method.
  • 2. Every new client request generates a new thread; that thread calls the service() method of your servlet (which may in turn call doPost(), doGet() and so forth).
  • 3. Under most circumstances, there is only one instance of your servlet, no matter how many client requests are in process. That means that at any given moment, there may be many threads running inside the service() method of your solo instance, all sharing the same instance data and potentially stepping on each other’s toes. This means that you should be careful to synchronize access to shared data (instance variables) using the synchronized keyword.
  • 4. Note that you need not (and should not) synchronize on local data or parameters. And especially you shouldn’t synchronize the service() method! (Or doPost(), doGet() et al.)
  • 5. A simple solution to synchronizing is to always synchronize on the servlet instance itself using “synchronized (this) { … }”. However, this can lead to performance bottlenecks; you’re usually better off synchronizing on the data objects themselves.
  • 6. If you absolutely can’t deal with synchronizing, you can declare that your servlet “implements SingleThreadModel”. This empty interface tells the web server to only send one client request at a time into your servlet. From the JavaDoc: “If the target servlet is flagged with this interface, the servlet programmer is guaranteed that no two threads will execute concurrently the service method of that servlet. This guarantee is ensured by maintaining a pool of servlet instances for each such servlet, and dispatching each service call to a free servlet. In essence, if the servlet implements this interface, the servlet will be thread safe.” Note that this is not an ideal solution, since performance may suffer (depending on the size of the instance pool), plus it’s more difficult to share data across instances than within a single instance.
  • 7. To share data across successive or concurrent requests, you can use either instance variables or class-static variables, or use Session Tracking.
  • 8. The destroy() method is not necessarily as clean as the init() method. The server calls destroy either after all service calls have been completed, or after a certain number of seconds have passed, whichever comes first. This means that other threads might be running service requests at the same time as your destroy() method is called! So be sure to synchronize, and/or wait for the other requests to quit. Sun’s Servlet Tutorial has an example of how to do this with reference counting.
  • 9. destroy() can not throw an exception, so if something bad happens, call log() with a helpful message (like the exception). See the “closing a JDBC connection” example in Sun’s Tutorial.

 

How do I use Session Tracking?

 

See section 2.3 of the Servlet Essentials tutorial (http://www.novocode.com/doc/servlet-essentials/) . Also see The Session Tracking API (http://webreview.com/wr/pub/1999/01/08/bookshelf/index.html) , excerpted from Java Servlet Programming (http://www.oreilly.com/catalog/jservlet/) by Jason Hunter (http://webreview.com/wr/pub/au/Hunter_Jason).

A point I haven’t seen emphasized enough is that you should only add objects that are serializable to an HttpSession. Specifically, a JDBC Connection object is not serializable, so should not be added to the session (despite the example in Jason Hunter’s otherwise admirable Java Servlet Programming). If you’d like to associate a connection with a session, then store some arbitrary, unique handle in the session, then use that to key off your own hashtable of connections. (Make sure upon retrieval that the returned connection is not null, and if it is, create a new connection!)

The reason is that sessions may, at the whim of the server, be swapped out to disk, in order to save memory or reboot the server. This behavior can be disabled by setting a configuration parameter in your server or servlet engine. (I can’t remember offhand what these are — please email me if you know.) From the spec: Some servlet engine implementations will persist session data or distribute it amongst multiple network nodes. For an object bound into the session to be distributed or persisted to disk, it must implement the Serializable interface.

 

How can I detect whether the user accepted my cookie?

 

Here’s a clever trick: use a redirect. Drop a cookie on the HttpServletResponse object, then call resp.sendRedirect() to a “phase two” servlet. The “phase two” servlet then tests whether the cookie was sent back to it. If so, that means the user accepted the cookie the first time; if not, it means that either the client rejected the cookie, or the browser doesn’t support cookies.

Note that this technique only works if the “phase two” servlet is hidden from the user; if the user can jump directly to the test phase, then the servlet can’t tell the difference between newly-arrived clients and cookie-unfriendly clients. That means you should send a redirect from the test phase, to make sure the user doesn’t have a chance to bookmark the test phase’s URL.

Alex Chaffee (http://www.stinky.com/alex/, alex@jguru.com) has written a Servlet that demonstrates this technique called CookieDetector (http://www.purpletech.com/code/CookieDetector.html)

 

How do I integrate HTML design into my Servlet?

 

The question can be rephrased, “How can I design a work flow that incorporates HTML designers and programmers to build a dynamically generated web site that will keep expanding and growing over time?” This is a special case of the intractable Content Management Problem of the Web in general. The real problem is to allow HTML designers (that is, humans) to use their favorite HTML editing tools without learning Java, and to allow marketing people (arguably humans) to change the look of the site on a whim, without having to alter the database access code inside the servlet. (And vice versa — to alter the business logic and data access without altering the user interface.)

See my article at Servlet Central (http://www.servletcentral.com/1998-12/designprocess.dchtml) for an expansion on these themes.

There are many, many possibilities… The list below is not complete, but should give you some guidelines.

A. Hardcode HTML. You can just put HTML inside of print statements in your Servlet’s doGet() or doPost() method.
Pro: easy to code, easy to understand for the programmer.
Con: difficult to understand for the designer; when a change has to be made, the programmer has to wait for the designer to finish her HTML, then re-hard-code it all back into print statements, then make sure the generated HTML actually does what the original HTML did. Basically, good for a hello world servlet, not good for a real web site.

B. Server Side Includes (SSI). Use the <SERVLET> tag inside your HTML file (and rename it .shtml). The HTML designers will make pretty pages; your servlets will output small pieces of text that get spliced in to the web page by the server.
Pro: separates UI (HTML) and code.
Con: You have two possible end paths with SSI: either your servlet outputs many tiny bits of text with no HTML tags, or your servlet outputs a big bunch of text with embedded HTML tags. In the first case, your code can’t take advantage of its knowledge of the structure of the data — for example, you can’t format an HTML table from a database. In the second case, you’re back to hardcoding HTML, thus making it hard to change the look of your pages.

C. Presentation Templates. This is a good way to put common navigation elements (button bar, credits, etc) on all of your pages. The technology is built in to the Java Web Server, and implemented by several (though not all) of the servlet engines.
Pro: you don’t have to enter in the same common HTML for all the countless pages in your web site.
Con: your servlet code still has to hardcode HTML if it wants to be powerful (see item B, SSI).

D. JSP Java Server Pages. You write files in HTML format, and embed actual Java code inside the HTML. This is kind of like using JavaScript, only it’s on the server, and it’s real Java. This is directly parallel to Microsoft’s ASP.
Pro: it’s really cool; you only need a single file to do both UI and layout code; you don’t have to type “println” so much.
Con: if you do anything interesting, then your HTML designers will get really confused looking at the interlaced Java and HTML code — so make sure to put the complicated code inside a JavaBean where it belongs, not in the JSP page.

The new version of the JSP spec has lots of features for integrating with JavaBeans, which is a great way to separate user interface (JSP) from data and business logic (beans). See also the JSP FAQ (see our References section for a link).

Halcyon Software has a product called Instant ASP, which allows you to execute Microsft IIS-style ASPs (including code in VBScript, Jscript, Perl, Java, and JavaScript) in any Servlet Engine. Also Live Software has CF_Anywhere, which executes Cold Fusion CFML pages. See the References section for links.

E. Write your own page parser. If for some reason you’re not happy with the standard mechanisms for doing templates (see B-D above), you can always write your own parser. Seriously. It’s not rocket science.

F. HTML Object Model Class Libraries e.g. htmlKona, XML. With these class libraries, you write code and build an object model, then let the objects export HTML. This doesn’t really work for complicated layouts — and forget about letting your designer use an HTML editor — but it can be useful when you have a highly dynamic site generating HTML, and you want to automate the process. Unfortunately, you still have to learn HTML, if only to understand and validate the output. See the References section of this FAQ for a listing of some class libraries that can help.

G. Do it all yourself Develop a database-driven content management system. Think C|Net. It has a lot of standard content, but the database is king. HTML designers have little pieces of the page that they can play with, but ultimately they’re just putting content into a database, and the site (servlet) is generating every page request dynamically. This sort of system is very difficult to design and build, but once you’ve built it, it can really pay off — but only if you have dozens of writers, editors, designers, and programmers all working on the same site on an ongoing basis.

For a brief list of alternate page template systems, see the References section of this FAQ.

 

How do I send email from a servlet?

 

From: James Cooper (pixel@bitmechanic.com) GSP and GnuJSP both come with SMTP classes that make sending email very simple. if you are writing your own servlet you could grab one of the many SMTP implementations from www.gamelan.com (search for SMTP and java). All the ones I’ve seen are pretty much the same — open a socket on port 25 and drop the mail off. so you have to have a mail server running that will accept mail from the machine JServ is running on.

See also the references section for a good list of Java email resources, including SMTP and POP classes.

 

Are there any ISPs that will host my servlets?

 

The Adrenaline Group maintains a list of ISP’s who host Java Servlets (http://www.adrenalinegroup.com/jwsisp.html) . Of these, a few have also said that they can host Java applications:

Daniel Kehoe (kehoe@fortuity.com) has had “very happy experiences” with Silicon Valley Web Hosting (http://www.svwh.net/) for several clients.

(http://www.servlets.net) is an ISP geared towards hosting servlets.

Please report any experiences, good or bad, you have with these services to &feedback;.

]

 

What is the difference between URL encoding and URL rewriting?

URL Encoding is a process of transforming user input to a CGI form so it is fit for travel across the network — basically, stripping spaces and punctuation and replacing with escape characters. URL Decoding is the reverse process. To perform these operations, call java.net.URLEncoder.encode() and java.net.URLDecoder.decode() (the latter was (finally!) added to JDK 1.2, aka Java 2).

Example: changing “We’re #1!” into “We%27re+%231%21″

URL Rewriting is a technique for saving state information on the user’s browser between page hits. It’s sort of like cookies, only the information gets stored inside the URL, as an additional parameter. The HttpSession API, which is part of the Servlet API, sometimes uses URL Rewriting when cookies are unavailable.

Example: changing <A HREF=”nextpage.html”> into
<A HREF=”nextpage.html;$sessionid$=DSJFSDKFSLDFEEKOE”> (or whatever the actual syntax is; I forget offhand)

There’s also a feature of the Apache web server called URL Rewriting; it is enabled by the mod_rewrite module. It rewrites URLs on their way in to the server, allowing you to do things like automatically add a trailing slash to a directory name, or to map old file names to new file names. This has nothing to do with servlets. For more information, see the Apache FAQ (http://www.apache.org/docs/misc/FAQ.html#rewrite-more-config) .

 

How can my applet communicate with my servlet?

 

It’s pretty straightforward. You can use the java.net.URLConnection and java.net.URL classes to open a standard HTTP connection to the web server. The server then passes this information to the servlet in the normal way. Basically, the applet pretends to be a web browser, and the servlet doesn’t know the difference. (Of course, you can write a servlet that is only called from your applet, in which case it *does* know the difference…)

For more detail, you can see the Sun Web Server FAQ (http://www.sun.com/software/jwebserver/faq/faq.html) Questions C8 (http://www.sun.com/software/jwebserver/faq/faq.html#c8) and C9 (http://www.sun.com/software/jwebserver/faq/faq.html#c9) . Also, Chad Darby has an article with source code (http://www.j-nine.com/pubs/applet2servlet/index.htm) on the subject. And Netscape DevEdge Online has a similar article – Applet-to-Servlet Communication for Enterprise Applications (http://developer.netscape.com/viewsource/index_frame.html?content=fields_servlet/fields_servlet.html) skip to the “Communication Tactics” section to get to the good part.

 

How can I debug my servlet?

 

Hoo boy, that’s a tough one.

First off, you should always do your own exception handling. An uncaught exception can silently kill your servlet, and if you don’t know where to look in the log files, or if your server has a bug in it whereby it silently swallows certain exceptions, you’ll have no idea where the trouble is.

The following code sets up a catch block that will trap any exception, and print its value to standard error output and to the ServletOutputStream so that the exception shows up on the browser (rather than being swallowed by the log file). Chances are that any error is in your code; the exception shows you what line the problem happened at. (If you see “Compiled Code” instead of line numbers in the exception stack trace, then turn off the JIT in your server.)

 

res.setContentType(”text/html”);

ServletOutputStream out = res.getOutputStream();

try {

  // do your thing here

  …

}

catch (Exception e) {

  StringWriter sw = new StringWriter();

  PrintWriter pw = new PrintWriter(sw);

  e.printStackTrace(pw);    

  out.println(”<pre>”);

  out.print(sw.toString());

  out.println(”</pre>”);           

}

Lately, I’ve started catching all Throwables, just in case. I know this is bad form but what are you gonna do?

Next, you should make liberal use of the log() method, and you should keep your own log files. Again, don’t trust the server to do the right thing. Also, printing the log after every request can help debugging because you can immediately see the output of your servlet without going into the server-side log files. (Another problem this avoids is that some servlet engines forget to flush their logs after each request, so even if you go to the server, you won’t see the most recent log messages.)

Here’s some source code you can add to any HttpServlet that keeps an in-memory log:

 

StringBuffer logBuffer = new StringBuffer();

 

public void log(String s) {

        s = new Date().toString() + “: ” + s;

        System.err.println(s);

        logBuffer.append(s);

        logBuffer.append(”\n”);

        super.log(s);

}

And here’s some code that you can add to the end of your doGet or doPost method that prints out the entire log after each request:

 

out.println(”<HR>\n<H3>Error log this session:</H3>\n<PRE>”);

out.println(logBuffer.toString());

out.println(”</PRE><P>”);

Both of these should be disabled once you actually ship your servlet, but they can be very useful during development and debugging.

You should remember to use servletrunner (renamed “JSDK WebServer” with the JSDK version 2 — run with the script startserver) to debug your servlet. It’s a tool that ships with the JSDK that basically starts up a miniature web server that runs your servlet inside itself. It means you don’t have to stop and restart your real web server every time you recompile your servlet. It also affords a more pure environment, so you can make sure your servlet truly conforms to the spec before you try to run it inside a (possibly buggy or nonstandard) servlet engine.

A few IDEs support servlet debugging. Symantec Cafe claims to have a fairly robust system for doing visual source-level debugging of servlets (as well as RMI, CORBA, and EJB objects). [If anyone has any experience with Cafe or other IDEs, please email &feedback;.] May Wone (abc408@hotmail.com) writes:

I am debugging servlets running servletRunner inside of IBM’s VisualAge for Java.

On my first servlet project, I used a ton of log messages for debugging.

This is my second servlet project, and this debugging technique has enhanced my productivity many folds. I am able to set code break points, step through each line of code, inspect all the objects visible to this class as well as view the objects in the current stack. I can also view, suspend, resume threads.

The setup instructions are at: (http://www.ibm.com/java/education/server-side/server-side.html)

jRun also claims to have excellent log file support as well as some debugging facilities.

Eric Gilbertson (eric@bitsource.com) adds:

Another way to debug servlets is to invoke JWS with java_g and then attach to the process using any debugger that is capable of attaching to a running Java process given a debug password. To do this invoke JWS as follows:

 

java_g.exe -debug -Dserver.name=”javawebserver” [extra args...] com.sun.server.ServerProcess

jdb password=[password]

This will start the Web server process and echo a password. The password may be then used with jdb to attach to the process. Note that this will start only the Web server, not the admin server.

Sun does not advertise this mechanism which in my mind is the only way to debug non-trivial servlets.

 

How do I create an image (GIF, JPEG, etc.) on the fly from a servlet? 

 

To create an image or do image processing from Java, there are several packages and classes available. See the References section for a list.

Once you have an image file in your servlet, you have two choices:

  • 1. Write the file to disk and provide a link to it. Make sure you write it to a location that’s in your web server directory tree (not just anywhere on the server’s disk). (Note that in some servlet engine setups, the servlet directory is not accessible by the web server, only by the servlet engine, which means you won’t be able to access it through an http:// URL.) You can either send an IMG tag in the HTML your servlet is outputting, or send an HTTP redirect to make the browser download the image directly (as its own page). (CookieDetector (http://www.purpletech.com/code/CookieDetector.html) has an example, with source code, of sending a redirect.)
    Pro: the image can be cached by the browser, and successive requests don’t need to execute the servlet again, reducing server load.
    Con: the image files will never be deleted from your disk, so you’ll either have to write a script to periodically clean out the images directory, or go in and delete them by hand. (Or buy a bigger hard disk :-) ).
  • 2. Output the image directly from the servlet. You do this by setting the Content-type header to image/gif (for GIFs), or image/jpeg (for JPEGs). You then open the HttpResponse output stream as a raw stream, not as a PrintStream, and send the bytes directly down this stream using the write() method.

Focus on Java (http://java.miningco.com/library/weekly/aa090299.htm) has a brief article describing the use of the Java 2 JPEGCodec class.

 

How do I upload a file to my servlet? 

From Thomas Moore’s Servlet FAQ:

Form-based file upload requires a couple of steps.

The server must supply (and the client must support) encoding type multipart/form-data. Most current browsers do, but it’s not a guarantee. Secondly (and this is usually the trickiest part), your servlet has to parse the binary data and do something with it (e.g., write it to a file on the server).

The intrepid programmer is referred to RFC 1867 for cluefulness on how to parse this data. Less brave souls can use either Jason Hunter’s implementation of a MultipartRequest (available from http://www.servlets.com), or CParseRFC1867 (available from http://www.servletcentral.com).

Note that the source code is available for both of these examples, but both assume that you will be writing the file to a file on the server. Other uses (e.g. storing the file as a binary object in a database) will require adaptation.

There is a multipart/form parser availailable from Anders Kristensen (http://www-uk.hpl.hp.com/people/ak/java/, ak@hplb.hpl.hp.com) at http://www-uk.hpl.hp.com/people/ak/java/#utils. JavaMail also has MIME-parsing routines (see the References section).

Here is an example of HTML code that allows file upload, courtesy of Detlef Pleiss (dpleiss@os-net.de):

 

<FORM ENCTYPE=”multipart/form-data” method=post

action=”…”> put the servlet URL here, of course

<INPUT TYPE=”file” NAME=”mptest”><INPUT TYPE=”submit” VALUE=”upload”>

</FORM>

The input type “file” brings up a button for a file select box on the browser together with a text field that takes the file name once selected. The servlet uses the GET method parameters to decide what to do with the upload while the POST body of the request contains the file data to parse. Tested with IE4, IE5 and Netscape 4.5.

 

How do I access a database from my servlet?

Since JDK 1.1, Java comes with a package called JDBC (Java Database Connectivity). JDBC allows you to write SQL queries as Java Strings, pass them to the database, and get back results that you can parse. To learn how to write JDBC code, check the tutorials on Sun’s web site, and read the Javadoc API documentation for package java.sql. To install JDBC on your system, you need to locate a JDBC Driver for your particular database and put it in your classpath. Fortunately, most databases these days ship with a 100% Pure Java driver (also known as a “Type IV” driver), including Oracle, Sybase, Informix, etc. Check the documentation for your database engine for installation instructions.